Date formatting with locale in Play Framework 2.2

别等时光非礼了梦想. 提交于 2019-12-05 18:23:38

In short, if you want to hard code the locale and use JodaTime:

@(date: org.joda.DateTime)
@import java.util.Locale
@date.format("yyyy-MMM-dd", new Locale("sv", "SE"))

if you want to use the locale selected from the browser lang header (you will also need the request implicitly to your template):

@(date: org.joda.DateTime)(implicit lang: play.api.i18n.Lang)
@date.format("yyyy-MMM-dd", lang.toLocale)

I wrote a detailed blog entry about this (since I have seen the question so many times):

https://markatta.com/codemonkey/blog/2013/10/14/formatted-localized-dates-in-playframework-2/

I found this here Google group (I own no credit for this but I thought it could help somebody)

@(myDate: org.joda.time.DateTime)

@import org.joda.time.format.DateTimeFormat

@defining(DateTimeFormat.forPattern("yyyy-MM-dd")) { dateFormatter =>
    @dateFormatter.print(myDate) )
}

alternatively you can have this

val dateFormatter = org.joda.time.format.DateTimeFormat.forPattern(myDatePattern)

and call it in the template as (assuming it is stored in utils and Object Format):

@utils.Format.dateFormatter.print(myDate)

works very well for me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!