簡體   English   中英

如何使用Anorm格式化DateTime?

[英]how can I format DateTime using Anorm?

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

}

從2.3.8版本的Anorm開始,結果解析器支持Joda和Java8時態類型。 參見Anorm列get[DateTime]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM