
本文介绍了如何将从上游系统获取的 EDT 时区时间字符串转换为 UTC 时间。通过使用 java.time 包中的类,如 ZonedDateTime 和 DateTimeFormatter,可以轻松地解析时间字符串并将其转换为 UTC 时间,以便存储到实体中。文章提供了详细的代码示例,展示了如何实现这一转换,并提供了相关的注意事项。
EDT 到 UTC 时间转换详解
在处理来自不同系统的数据时,经常会遇到时区转换的问题。本文将详细介绍如何将 EDT (Eastern Daylight Time) 时区的时间字符串转换为 UTC (Coordinated Universal Time) 时间,并提供 Java 代码示例。
使用 java.time 包
Java 8 引入了 java.time 包,它提供了强大的日期和时间处理功能。我们可以使用该包中的类来完成时区转换。
核心类包括:
- ZonedDateTime: 表示带时区的日期和时间。
- DateTimeFormatter: 用于解析和格式化日期和时间。
- ZoneId: 表示时区 ID。
- Instant: 时间线上的瞬时点,代表 UTC 时间。
代码示例
以下是一个完整的示例,演示了如何将 EDT 时间字符串转换为 UTC 时间:
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String dateFromUpstream = "11-14-2022 10:41:12 EDT";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-yyyy HH:mm:ss z", Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(dateFromUpstream, dtf);
Instant instant = zdt.toInstant();
System.out.println(instant);
// Or get a ZonedDateTime at UTC
ZonedDateTime zdtUTC = zdt.withZoneSameInstant(ZoneOffset.UTC);
System.out.println(zdtUTC);
// If you want LocalDateTime
LocalDateTime ldt = zdtUTC.toLocalDateTime();
System.out.println(ldt);
}
}代码解释:
- 定义时间字符串和格式化器: 首先,定义包含 EDT 时间的字符串 dateFromUpstream。然后,创建一个 DateTimeFormatter 对象 dtf,用于解析该字符串。DateTimeFormatter.ofPattern("MM-dd-yyyy HH:mm:ss z", Locale.ENGLISH) 指定了日期时间字符串的格式,其中 "z" 表示时区缩写。Locale.ENGLISH 确保时区缩写能够被正确解析。
- 解析时间字符串: 使用 ZonedDateTime.parse(dateFromUpstream, dtf) 将时间字符串解析为 ZonedDateTime 对象 zdt。
-
转换为 UTC 时间:
- 方法一:转换为 Instant: 使用 zdt.toInstant() 将 ZonedDateTime 转换为 Instant 对象 instant。 Instant 表示 UTC 时间。
- 方法二:转换为 ZonedDateTime (UTC): 使用 zdt.withZoneSameInstant(ZoneOffset.UTC) 将 ZonedDateTime 的时区转换为 UTC,得到新的 ZonedDateTime 对象 zdtUTC。 withZoneSameInstant 方法会调整日期和时间,以保持相同的瞬时点,但使用新的时区。
- 转换为 LocalDateTime (可选): 如果需要将 UTC 时间表示为 LocalDateTime 对象,可以使用 zdtUTC.toLocalDateTime()。 注意,LocalDateTime 不包含时区信息。
输出结果:
2022-11-14T15:41:12Z 2022-11-14T15:41:12Z 2022-11-14T15:41:12
替代方案:使用 OffsetDateTime
正如 Basil Bourque 建议的,也可以使用 OffsetDateTime 来进行转换:
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String dateFromUpstream = "11-14-2022 10:41:12 EDT";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM-dd-yyyy HH:mm:ss z", Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(dateFromUpstream, dtf);
OffsetDateTime odtUTC = zdt.toOffsetDateTime()
.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println(odtUTC);
}
}此代码首先将 EDT 时间字符串解析为 ZonedDateTime,然后将其转换为 OffsetDateTime,最后使用 withOffsetSameInstant 方法将偏移量设置为 UTC。
输出结果:
2022-11-14T15:41:12Z
注意事项
- 时区 ID: 确保使用正确的时区 ID。 例如,ZoneId.of("America/New_York") 表示纽约时区。
- 日期时间格式: DateTimeFormatter 的模式字符串必须与输入的时间字符串格式完全匹配。 否则,解析会失败。
- Locale 的使用: 在解析包含时区缩写的日期时间字符串时,建议指定 Locale.ENGLISH,以确保时区缩写能够被正确解析。
- 存储到实体: 根据实体的类型,选择合适的日期时间类型进行存储。 如果需要存储时区信息,可以使用 ZonedDateTime 或 OffsetDateTime。 如果只需要存储日期和时间,可以使用 LocalDateTime,但需要注意时区信息的丢失。
总结
本文介绍了如何使用 java.time 包将 EDT 时间字符串转换为 UTC 时间。 通过使用 ZonedDateTime 和 DateTimeFormatter,可以轻松地完成时区转换。在实际应用中,需要根据具体的需求选择合适的日期时间类型进行存储。 建议查阅 Trail: Date Time 以了解更多关于 java.time API 的信息。










