Posted in

在 Java 中转换 ZonedDateTime 和 Date_AI阅读总结 — 包阅AI

包阅导读总结

1. `Java`、`ZonedDateTime`、`Date`、`转换`、`时间管理`

2. 本文介绍了在 Java 中 ZonedDateTime 和 Date 之间的转换,包括对相关类的理解,如 LocalDateTime、ZonedDateTime、Instant 等,还通过示例代码展示了两者相互转换的方法,并对输出结果进行了说明。

3.

– 理解不同的时间类

– `Date`:以毫秒表示自纪元以来的日期和时间,无时区信息。

– `LocalDateTime`:无时区的日期和时间组合。

– `ZonedDateTime`:具有时区的日期和时间。

– `Instant`:无时区的时间点,常用于时间比较。

– `ZonedDateTime` 转 `Date`

– 先将 `ZonedDateTime` 转 `Instant`,再用 `Date` 的静态方法从 `Instant` 创建 `Date` 对象。

– `Date` 转 `ZonedDateTime`

– 先将 `Date` 转 `Instant`,再用 `ZonedDateTime` 的静态方法结合 `Instant` 和 `ZoneId` 创建新对象。

– 结论

– 探讨了类的差异,提供了转换示例。

思维导图:

文章地址:https://www.javacodegeeks.com/converting-between-zoneddatetime-and-date-in-java.html

文章来源:javacodegeeks.com

作者:Omozegie Aziegbe

发布时间:2024/7/2 12:52

语言:英文

总字数:638字

预计阅读时间:3分钟

评分:86分

标签:日期转换,即时,本地日期时间,时区,ZonedDateTime


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

In Java, managing date and time can involve converting between different types, such as ZonedDateTime and Date. This is particularly useful when dealing with legacy code or APIs that use the older Date class or when integrating with libraries that expect specific types. This article provides a guide on converting between ZonedDateTime and Date in Java.

1. Understanding Date, LocalDateTime, ZonedDateTime, and Instant

Firstly, let’s understand the LocalDateTime, ZonedDateTime, and Instant classes, as they seem quite similar. It’s important to know their differences.

  • java.util.Date class represents a date and time in milliseconds since the epoch (January 1, 1970, 00:00:00 GMT). It lacks time zone information.
  • LocalDateTime represents a date and time without a time zone. It combines LocalDate and LocalTime.
  • ZonedDateTime represents a date and time with a time zone. It is useful for applications that need to work with time zones.
  • Instant represents a point in time (a timestamp) without any time zone information. It is often used for time comparisons.

Example Usage

import java.time.*;public class DateTimeExamples {    public static void main(String[] args) {                LocalDateTime localDateTime = LocalDateTime.now();        ZonedDateTime zonedDateTime = ZonedDateTime.now();        Instant instant = Instant.now();                System.out.println("LocalDateTime: " + localDateTime);        System.out.println("ZonedDateTime: " + zonedDateTime);        System.out.println("Instant: " + instant);            }}

Output

Differences in Output

  • LocalDateTime: The output does not include any time zone information. It simply represents the local date and time.
  • ZonedDateTime: The output includes both the date and time along with the time zone offset and the time zone ID, indicating the specific time zone in which the date and time are represented.
  • Instant: The output represents a specific point in time in UTC, indicated by the “Z” (which stands for Zulu time, or UTC).

2. Convert ZonedDateTime to Date

Converting from ZonedDateTime to Date can be achieved in two steps:

  • Convert ZonedDateTime to an Instant: We can use the toInstant() method of ZonedDateTime to get the corresponding Instant.
  • Convert Instant to Date: The Date class has a static method from(Instant) that allows us to create a Date object from an Instant.

Example:

import java.time.ZonedDateTime;import java.util.Date;public class ZonedDateTimeToDate {    public static void main(String[] args) {                // Create a ZonedDateTime object        ZonedDateTime zonedDateTime = ZonedDateTime.now();        // Convert ZonedDateTime to Date        Date date = Date.from(zonedDateTime.toInstant());        // Display the converted Date        System.out.println("ZonedDateTime: " + zonedDateTime);        System.out.println("Date: " + date);    }}

Output

ZonedDateTime: 2024-07-01T20:22:41.837597+01:00[Africa/Lagos]Date: Mon Jul 01 20:22:41 WAT 2024

3. Convert Date to ZonedDateTime

Converting from Date to ZonedDateTime requires specifying a time zone. Since Date doesn’t have time zone information, we need to define the desired zone for the resulting ZonedDateTime. Here is the approach:

  • Convert Date to Instant: Similar to the previous conversion, use the toInstant() method of the Date class to get the corresponding Instant.
  • Create ZonedDateTime with Instant and ZoneId: Use the ofInstant(Instant, ZoneId) static method of ZonedDateTime to create a new object with the obtained Instant and the desired ZoneId.

Example:

import java.time.ZoneId;import java.time.ZonedDateTime;import java.util.Date;public class DateToZonedDateTime {    public static void main(String[] args) {               // Create a Date object        Date date = new Date();        // Convert Date to ZonedDateTime        ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneId.systemDefault());        // Display the converted ZonedDateTime        System.out.println("Date: " + date);        System.out.println("ZonedDateTime: " + zonedDateTime);            }}

Here, we converted Instant to ZonedDateTime using atZone(zoneId) method, specifying the desired time zone (ZoneId.systemDefault() in this example).

Output

Date: Tue Jul 02 08:11:39 WAT 2024ZonedDateTime: 2024-07-02T08:11:39.625+01:00[Africa/Lagos]

4. Conclusion

In this article, we explored the differences between LocalDateTime, ZonedDateTime, and Instant in Java and provided code examples to illustrate how these classes function and how to convert between them, particularly focusing on converting ZonedDateTime to Date and vice versa.

5. Download the Source Code

This article covered Java ZonedDateTime to Date conversion.