【问题标题】:Date conversion according to user timezone [duplicate]根据用户时区进行日期转换[重复]
【发布时间】:2019-06-15 13:57:56
【问题描述】:

我们正在将数据库日期转换为所需的用户时区。但是,如果我们以字符串格式使用 jodatime 进行格式化,我们将获得正确的日期,如果我们解析字符串以获取日期对象,我们将获得错误的时间。这是我的代码。我尝试了 jodatime 解析器和 java 日期

public class Test {

    public static void main(String[] args) {
        String dateF = "01/19/2019 at 07:35 PM (UTC)";
        String dateFormat = "MM/dd/yyyy 'at' hh:mm a (zzz)";
        long time = 1603305000;
        String timeZone = "Etc/UTC";
        Locale locale=new Locale("en", "US");
        DateTimeFormatter dateFormatter = null;

        if (locale.equals(Locale.FRENCH)) {
            dateFormatter = DateTimeFormat.forPattern(dateFormat).withLocale(locale);
        } else {
            dateFormatter = DateTimeFormat.forPattern(dateFormat).withLocale(null);

        }

        if (true) {
            dateFormatter = dateFormatter.withZone(DateTimeZone.forID(timeZone));
        }
        // Old Logic using Java Time
        DateFormat format3 = new SimpleDateFormat(dateFormat, locale);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        if(true)
            format3.setTimeZone(TimeZone.getTimeZone(timeZone));

        DateTime jodatime = new DateTime(time);
        try {
            System.out.println(dateFormatter.print(jodatime));
            System.out.println("timezone converted Date : " + format3.parse(dateFormatter.print(jodatime)));

            System.out.println("dateFormatter.parseDateTime converted Date : " + dateFormatter.parseDateTime(dateFormatter.print(jodatime)).toDate());
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

格式化字符串01/19/1970 at 01:21 PM (UTC)中的正确日期

解析后我们得到了错误的结果 时区转换日期:Mon Jan 19 18:51:00 IST 1970

dateFormatter.parseDateTime 转换日期:Mon Jan 19 18:51:00 IST 1970

格式化日期中的正确日期:Mon Jan 19 01:21:00 UTC 1970

【问题讨论】:

  • 您在使用 Joda-Time 时为什么要使用 SimpleDateFormat?为什么您使用已弃用的 Joda-Time,而不是 Java Time API?
  • 仅供参考,非常麻烦的旧日期时间类,例如 java.util.Date、java.util.Calendar 和 java.text.SimpleDateFormat 现在是 legacy,被 Java 8 中内置的 java.time 类所取代,之后。见Tutorial by Oracle。
  • 仅供参考,Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。
  • 根据Epoch Unix Time Stamp Converter Unix 时间 1603305000 相当于 10/21/2020 @ 6:30pm (UTC)。您确定要 1970 年 1 月 19 日下午 1:21 (UTC) 吗?
  • 1 月 19 日星期一 18:51:00 IST 1970 与 1970 年 1 月 19 日下午 1:21 (UTC) 一致。如果我假设 IST 表示印度标准时间(它可以有其他含义)。两次您都在打印 java.util.Date 对象,而 Date 对象没有时区,因此您不应期望将其转换为所需的用户时区。

标签: java datetime timezone jodatime


【解决方案1】:

避免使用旧的日期时间类

您正在使用与 Java 的最早版本捆绑在一起的糟糕的旧类。这些已被现代 java.time 类完全取代。

您显然也将这些遗留类与 Joda-Time 库中的类混合在一起。首先,这种混合是不明智的。其次,Joda-Time 现在处于维护模式,其创建者建议迁移到 java.time。实际上,Joda-Time 和 java.time 项目都是由同一个人 Stephen Colebourne 领导的,第一个项目是第二个项目的灵感和教育。

智能对象,而不是哑字符串

我们正在转换数据库日期

然后避免你正在做的所有字符串操作。

从 JDBC 4.2 开始,您可以直接与数据库交换 java.time 对象。暂时,意味着类似于 SQL 标准 TIMESTAMP WITH TIME ZONE 类型的列,使用 OffsetDateTime 类。

myPreparedStatement.setObject( … , myOffsetDateTime ) ;

……和……

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;

大多数数据库以 UTC 格式存储时刻。您可能希望调整到一个时区以向用户展示。应用ZoneId 以获取ZonedDateTime 对象。

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;

使用DateTimeFormatter 对象生成特定格式的文本。指定自定义格式模式或通过调用 ofLocalized… 方法自动本地化。

所有这些都已经在 Stack Overflow 上进行了很多次介绍。搜索以了解更多信息。


关于java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date、Calendar 和 SimpleDateFormat。

Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。

您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。

从哪里获得 java.time 类?

ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如Interval、YearWeek、YearQuarter 和more。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 2020-07-18
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    相关资源
    最近更新 更多