我使用PlayFramework和Anorm来获取存储在我的数据库中的日期列表:
def getDatesFromDB(): List[Array[String]] = {
db.withConnection { implicit c =>
SQL("Select [dateStart] from [dbo].[dates]").as(scalar[Array[String]].*);
}
}但是知道我的日期存储在date类型中,如何才能获得DateTime格式的列表呢?例如: 2016-02-11
最后,我想要一个这样的列表:
new DateTime("2016-01-17"),
new DateTime("2016-01-20"),
new DateTime("2016-01-23"),
....谢谢
发布于 2016-05-20 10:43:57
如果想在scala中格式化DateTime,只需使用toString方法,如下所示
val myDate = DateTime.now()
val myFormatDate = myDate.toString("yyyy-MM-dd")至于你的问题,你可以先得到ArrayDateTime格式的结果,然后把DateTime改成你想要的格式。
def getDatesFromDB(): List[Array[String]] = {
val result_tmp = db.withConnection { implicit c =>
SQL("Select [dateStart] from [dbo].[dates]").as(scalar[Array[String]].*)
}
// Referred to your code, the type of result_tmp should be List[Array[DateTime]]
// we can format it as you want with the following code
val result = result_tmp.map { item =>
item.map { myDate => myDate.toString("yyyy-MM-dd")
}
result
}祝好运
https://stackoverflow.com/questions/37334223
复制相似问题