如何使用 Anorm 格式化 DateTime?
how can I format DateTime using Anorm?
我从 mysql 得到 "time" 并需要将其渲染为 fe,但我得到时间戳形式的 "time" 我希望它显示为 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
}
}
从 Anorm 2.3.8 版本开始,结果解析器支持 Joda 和 Java8 时间类型;参见 Anorm column:get[DateTime]
。
我从 mysql 得到 "time" 并需要将其渲染为 fe,但我得到时间戳形式的 "time" 我希望它显示为 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
}
}
从 Anorm 2.3.8 版本开始,结果解析器支持 Joda 和 Java8 时间类型;参见 Anorm column:get[DateTime]
。