java - 根据用户时区进行日期转换

标签 java datetime timezone jodatime

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

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

最佳答案

避免遗留日期时间类

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

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

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

We are converting database date

然后避免您正在执行的所有字符串操作。

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

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 driver符合JDBC 4.2或稍后。不需要字符串,不需要 java.sql.* 类。

从哪里获取java.time类?

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

关于java - 根据用户时区进行日期转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54302484/

相关文章:

templates - Freemarker 在模板中打印日期

mysql - MySQL中的日期字段,查找所有不重叠的行并仅返回差异

java - 将时分字符串时间转换为 utc,反之亦然

javascript - Plotly.js 设置类型 :"date"的时区

linux - 找出特定日期时间自 unix 纪元以来的时间?

java - 使用 GridFSOperations 获取所有文档

java - 如何使用充气城堡签署签名数据?

java - Java中的已用和空闲内存

java - PreparedStatement、浮点值和 postgresql

c# - .NET LINQ 按日期(天)对实体进行分组