如何使用Anorm格式化DateTime



我从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
  }

}

由于Anorm的2.3.8版本,Joda&结果解析器支持Java8时态类型;参见Anorm列:get[DateTime]

相关内容

  • 没有找到相关文章

最新更新