java - Play Framework 2.2 中使用区域设置的日期格式

标签 java date playframework string-formatting playframework-2.2

根据我在自动生成的 application.conf 文件中看到的内容,Play Framework 2.2 中的日期/时间根据 date.format 中的定义进行格式化那个文件。例如,我定义了

date.format=yyyy-MM-dd
date.format.dk=d. MMMM yyyy

但是,在 Scala 模板中打印日期时,框架似乎忽略了这些值。 This thread 提供了一种解决方案,其中将模式作为 myDate.format("yyyy-MM-dd") 直接输入到模板中。 (如果使用 Jodatime,我猜这会变成 myDate.toDate().format("yyyy-MM-dd") 因为 上没有定义 format() >DateTime 类。)但这不仅会在每次显示日期时强制重复该模式,还会忽略当前的语言环境。

那么在 Play Framework 2.2.x 中针对不同语言环境格式化日期和时间的预期方式是什么?

最佳答案

简而言之,如果您想对区域设置进行硬编码使用 JodaTime:

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

如果你想使用从浏览器 lang header 中选择的语言环境(你还需要隐式请求到你的模板):

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

我写了一篇关于这个的详细博客条目(因为我已经多次看到这个问题):

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

关于java - Play Framework 2.2 中使用区域设置的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19335705/

相关文章:

java - 加载 JDBC 驱动程序

java - 处理 recyclerview 单击 fragment 而不是 holder 类

python - 将数据框中的日期拆分为 2 个单独的数据框

php - SQL ORDER BY date DESC 不返回最后一条记录

java - 非法尝试将代理与两个打开的 session 相关联

java - lombok 包不存在

python - Pandas 从非统一的日期列表中查找一年前的日期

java - Play Framework : Created WAR file is not zipped up

scala - Play Framework 静态文件的 nginx 配置

scala - 使用 Scala 在 Play 框架中组合 `Future` 结果