ms-*_*-tg 3 mysql timezone playframework-2.0
我们希望我们的Play Framework 2.0 Scala应用程序能够在应用程序服务器和MySQL数据库服务器中处理UTC中的所有日期和时间信息.
诀窍是:
这样做有标准的最佳做法吗?我们希望测试以UTC格式运行,而不必传递-Duser.timezone=GMT所有命令行.用于启动服务器的同上play start.
这比我们预期的要容易.
首先,使用另一个StackOverflow问题中描述application.conf的参数配置JDBC URL :
# Set MySQL Connector/J to use UTC server connection and time conversions
# see https://stackoverflow.com/questions/10488529/gettimestamp-does-timezone-converstion-twice-in-mysql-jdbc-connector
db.default.url="jdbc:mysql://localhost/database?useGmtMillisForDatetimes=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&useTimezone=true&serverTimezone=UTC"
Run Code Online (Sandbox Code Playgroud)
其次,在Build.scala,设置Java系统属性和默认值:
// Setting this property here forces the JVM to run in UTC time,
// both for test (`play test`) and development (`play start`) modes,
// but not for production (`play dist`).
System.setProperty("user.timezone", "GMT")
TimeZone.setDefault(TimeZone.getTimeZone("GMT"))
Run Code Online (Sandbox Code Playgroud)
这两个更改将同时处理test(play test)和development(play start)模式.
对于production(play dist),必须在启动之前设置属性.例如,通过:
start脚本以进行添加export _JAVA_OPTIONS=-Duser.timezone=GMTstart脚本调用脚本-Duser.timezone=GMTSystem.setProperty("user.timezone", "GMT")| 归档时间: |
|
| 查看次数: |
3336 次 |
| 最近记录: |