2015-05-15 52 views
0

我從MySQL獲得「時間」並需要將其渲染到fe,但我得到時間戳形式的「時間」,我希望它顯示爲yyyy-MM-dd HH:mm:ss,我怎麼能在服務器代碼中執行如下操作?如何使用Anorm格式化DateTime?

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 
    } 

}

+1

更多的側面說明,但該代碼看起來很容易受到sql注入的影響,如果類別可以由外部調用者指定。 –

回答

1

由於ANORM的2.3.8版本,約達& Java8顳類型在結果解析器支撐;請參閱Anorm columnget[DateTime]

+0

沒有什麼能阻止你在解析器的'.map'中格式化'DateTime'。 – cchantep