【问题标题】:Joda : UTC to User's locale with daylight saving offsetJoda:UTC 到用户的区域设置与夏令时偏移
【发布时间】:2015-01-04 01:57:21
【问题描述】:

我的 PostgreSQL 字段中有一个 java.util.Date 存储为 2014-11-08 13:45:00。 (这是UTC)

我想显示它:至少使用“欧洲/巴黎”时区,应该下注 14:45

我试过了

DateTimeFormat.fullDateTime().withZone(DateTimeZone.forID("Europe/Paris")).print(new DateTime(myJavaDate));

还有一些其他类似这样的组合没有成功。

更好的是,如果它可以使用动态Locale 而不是硬编码的TimeZone,那将是完美的。

谢谢

编辑: 要在 DB 中编写它,我使用 Ebean:

SimpleDateFormat parserSDF=new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aaa", Locale.ENGLISH);
Date myJavaDate = parserSDF.parse(strDate);
...
bean.date = myJavaDate;
bean.save();

在我的表中,它是一个 timestamp without time zone 列(Ebean 自动生成)

【问题讨论】:

  • 在PostgreSQL中是如何存储的?时区信息是否也与日期一起存储?
  • 感谢您的回答。我解析一个字符串以获取 java.util.Date,然后将它与 Ebean 一起存储(请参阅我的编辑)
  • @fge 实际上,Postgres 从不使用日期时间值存储时区信息。 SQL 类型TIMESTAMP WITH TIME ZONE 用词不当。真正的含义是“尊重时区”,因为它使用任何传递的时区信息来调整到 UTC。另一个 SQL 类型 TIMESTAMP WITHOUT TIME ZONE 忽略任何传递的时区偏移信息(顺便说一句,这是个坏主意)。

标签: java postgresql timezone jodatime playframework-2.2


【解决方案1】:

我的工作解决方案

public String getTime(){
    Locale locale = Controller.request().acceptLanguages().get(0).toLocale();

    DateTime dateTime = new DateTime(betDate, DateTimeZone.UTC);
    dateTime = dateTime.withZone(DateTimeZone.forID("Europe/Paris"));
    return DateTimeFormat.shortTime().withLocale(locale).print(dateTime);
}

public String getDate(){
    Locale locale = Controller.request().acceptLanguages().get(0).toLocale();

    DateTime dateTime = new DateTime(betDate, DateTimeZone.UTC);
    dateTime = dateTime.withZone(DateTimeZone.forID("Europe/Paris"));
    return DateTimeFormat.shortDate().withLocale(locale).print(dateTime);
}

【讨论】:

    猜你喜欢
    • 2014-05-06
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 2016-11-25
    • 1970-01-01
    • 2016-04-09
    • 2020-01-19
    相关资源
    最近更新 更多