[英]how can I format DateTime using Anorm?
I get the "time" from mysql and need to render it to fe, but I get the "time" in the form of timestamp and I want it displayed as yyyy-MM-dd HH:mm:ss, how can I do it in the server code as follows? 我从mysql获取“时间”并需要将其呈现给fe,但我以时间戳的形式获取“时间”,并且希望将其显示为yyyy-MM-dd HH:mm:ss,我该怎么办在服务器代码如下?
object JDItem {
val jditem = {
get[Long]("id") ~
get[String]("name") ~
get[String]("url") ~
get[Double]("price") ~
get[Int]("commentnum") ~
get[Int]("likerate") ~
get[DateTime]("time") ~
get[String]("category") map {
case id ~ name ~ url ~price~
commentnum~likerate~time~category => JDItem(id,name,url,price,
commentnum,likerate,time,category)
}
}
implicit val JDItemWrites: Writes[JDItem] = (
(JsPath \ "id").write[Long] and
(JsPath \ "name").write[String] and
(JsPath \ "url").write[String] and
(JsPath \ "price").write[Double] and
(JsPath \ "commentnum").write[Int] and
(JsPath \ "likerate").write[Int] and
(JsPath \ "time").write[DateTime] and
(JsPath \ "category").write[String]
)(unlift(JDItem.unapply))
def getJson(category:String,sort:String,DescOrAsc:String):JsObject = DB.withConnection{ implicit c =>
val list = SQL("select id,name,url,price,commentnum,likerate,time,category from "+category+" order by "+sort+" "+DescOrAsc+" limit 100").as(jditem *)
val json:JsValue = Json.toJson(list)
val jsobject = Json.obj("total"-> list.length,"rows"-> json)
jsobject
}
} }
Since version 2.3.8 of Anorm, Joda & Java8 temporal types are supported in result parser; 从2.3.8版本的Anorm开始,结果解析器支持Joda和Java8时态类型。 see Anorm column :
get[DateTime]
. 参见Anorm列 :
get[DateTime]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.