Converting Java Date to OffsetDateTime – 将Java日期转换为OffsetDateTime

最后修改: 2019年 10月 27日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we learn about the difference between Date and OffsetDateTime. We also learn how to convert from one to the other.

在本教程中,我们学习了DateOffsetDateTime之间的区别。我们还学习了如何从一个转换到另一个。

2. Difference Between Date and OffsetDateTime

2.DateOffsetDateTime之间的区别

OffsetDateTime was introduced in JDK 8 as a modern alternative to java.util.Date.

OffsetDateTime在JDK 8中被引入,作为java.util.Date的现代替代品。

OffsetDateTime is a thread-safe class that stores date and time to a precision of nanoseconds. Date, on the other hand, is not thread-safe and stores time to millisecond precision.

OffsetDateTime是一个线程安全的类,它以纳秒的精度来存储日期和时间。Date则不是线程安全的,它以毫秒级的精度存储时间。

OffsetDateTime is a value-based class, which means that we need to use equals when comparing references instead of the typical ==.

OffsetDateTime是一个基于值的类,这意味着我们需要在比较引用时使用equals ,而不是典型的==。

The output of OffsetDateTime‘s toString method is in ISO-8601 format, while Date‘s toString is in a custom non-standard format.

OffsetDateTimetoString方法的输出是ISO-8601格式,而DatetoString是自定义非标准格式。

Let’s call toString on of both classes to see the difference:

让我们在这两个类中调用toString来看看区别。

Date: Sat Oct 19 17:12:30 2019
OffsetDateTime: 2019-10-19T17:12:30.174Z

Date can’t store timezones and corresponding offsets. The only thing that a Date object contains is the number of milliseconds since 1 January 1970, 00:00:00 UTC, so if our time isn’t in UTC we should store the timezone in a helper class. On the contrary, OffsetDateTime stores the ZoneOffset internally.

Date不能存储时区和相应的偏移量。Date对象所包含的唯一内容是自1970年1月1日00:00:00 UTC以来的毫秒数,所以如果我们的时间不是在UTC,我们应该在一个辅助类中存储时区。相反,OffsetDateTime内部存储ZoneOffset

3. Converting Date to OffsetDateTime

3.将Date转换为OffsetDateTime

Converting Date to OffsetDateTime is pretty simple. If our Date is in UTC, we can convert it with a single expression:

Date转换为OffsetDateTime相当简单。如果我们的Date是UTC,我们可以用一个表达式来转换它。

Date date = new Date();
OffsetDateTime offsetDateTime = date.toInstant()
  .atOffset(ZoneOffset.UTC);

If the original Date isn’t in UTC, we can provide the offset (stored in a helper object, because as mentioned earlier Date class can’t store timezones).

如果原始的Date不是UTC,我们可以提供偏移量(存储在一个辅助对象中,因为如前所述Date类不能存储时区)。

Let’s say our original Date is +3:30 (Tehran time):

假设我们最初的Date是+3:30(德黑兰时间)。

int hour = 3;
int minute = 30;
offsetDateTime = date.toInstant()
  .atOffset(ZoneOffset.ofHoursMinutes(hour, minute));

OffsetDateTime provides many useful methods that can be used afterward. For example, we can simply getDayOfWeek(), getDayOfMonth(), and getDayOfYear(). It’s also very easy to compare two OffsetDateTime objects with isAfter and isBefore methods.

OffsetDateTime提供了许多有用的方法,可以在之后使用。例如,我们可以简单地getDayOfWeek()getDayOfMonth()getDayOfYear()。isAfterisBefore方法来比较两个OffsetDateTime对象也非常容易。

Above all, it’s a good practice to avoid the deprecated Date class entirely.

最重要的是,完全避免使用废弃的Date类是一个好的做法。

4. Conclusion

4.总结

In this tutorial, we learned how simple it is to convert from Date to OffsetDateTime.

在本教程中,我们了解到从Date转换为OffsetDateTime是多么简单。

And, as always, the code is available over on Github.

而且,像往常一样,代码可以在Github上获得。