Java ZoneOffsetTransitionRule.TimeDefinition 枚举
最后修改时间:2025 年 4 月 16 日
ZoneOffsetTransitionRule.TimeDefinition
枚举定义了在时区转换期间如何解释本地时间。 它是 java.time.zone
包的一部分,用于夏令时转换。
此枚举指定在转换期间,本地时间应被解释为 UTC 时间、挂钟时间还是标准时间。 它有助于确定时区偏移量更改发生的准确时刻。 该枚举由 Java 时间 API 内部使用。
TimeDefinition 枚举概述
TimeDefinition
枚举有三个可能的值,定义了如何在转换期间解释本地时间。 每个值都为在转换期间在本地时间和 UTC 之间进行转换提供了不同的行为。
public enum TimeDefinition { UTC, WALL, STANDARD; public abstract OffsetDateTime createDateTime( LocalDateTime dateTime, ZoneOffset standardOffset, ZoneOffset wallOffset); }
上面的代码显示了枚举声明及其值。 每个值定义了在时区偏移量转换期间解释本地时间的不同方式。 createDateTime
方法实现了特定的转换逻辑。
UTC 时间定义
UTC
值表示在转换期间本地时间应被解释为 UTC 时间。 这是最简单的情况,其中本地时间与 UTC 完全匹配。 不会应用任何偏移量调整。
import java.time.*; import java.time.zone.*; public class Main { public static void main(String[] args) { LocalDateTime localTime = LocalDateTime.of(2025, 3, 10, 2, 0); ZoneOffset standardOffset = ZoneOffset.ofHours(-5); ZoneOffset wallOffset = ZoneOffset.ofHours(-4); OffsetDateTime result = TimeDefinition.UTC.createDateTime( localTime, standardOffset, wallOffset); System.out.println("UTC interpretation: " + result); } }
此示例显示了 UTC 解释的工作原理。 本地时间被视为 UTC 时间,因此生成的 OffsetDateTime 不会应用额外的偏移量。 当转换时间以 UTC 指定时,这很有用。
WALL 时间定义
WALL
值表示在转换期间本地时间应被解释为挂钟时间。 这是夏令时更改最常见的情况,其中本地时钟向前或向后移动。
import java.time.*; import java.time.zone.*; public class Main { public static void main(String[] args) { LocalDateTime localTime = LocalDateTime.of(2025, 3, 10, 2, 0); ZoneOffset standardOffset = ZoneOffset.ofHours(-5); ZoneOffset wallOffset = ZoneOffset.ofHours(-4); OffsetDateTime result = TimeDefinition.WALL.createDateTime( localTime, standardOffset, wallOffset); System.out.println("WALL interpretation: " + result); } }
此示例演示了 WALL 时间解释。 本地时间被视为挂钟时间,因此应用了挂钟偏移量(-4 小时)。 这表示夏令时期间时钟上显示的实际时间。
STANDARD 时间定义
STANDARD
值表示在转换期间本地时间应被解释为标准时间。 这使用标准偏移量,而无需夏令时调整。
import java.time.*; import java.time.zone.*; public class Main { public static void main(String[] args) { LocalDateTime localTime = LocalDateTime.of(2025, 3, 10, 2, 0); ZoneOffset standardOffset = ZoneOffset.ofHours(-5); ZoneOffset wallOffset = ZoneOffset.ofHours(-4); OffsetDateTime result = TimeDefinition.STANDARD.createDateTime( localTime, standardOffset, wallOffset); System.out.println("STANDARD interpretation: " + result); } }
此示例显示了 STANDARD 时间解释。 本地时间被视为标准时间,因此应用了标准偏移量(-5 小时)。 这表示没有夏令时调整的时间。
创建转换规则
TimeDefinition
在创建时区偏移量转换规则时使用。 这些规则定义了一年中时区偏移量更改的方式和时间。
import java.time.*; import java.time.zone.*; public class Main { public static void main(String[] args) { ZoneOffsetTransitionRule rule = ZoneOffsetTransitionRule.of( Month.MARCH, 10, DayOfWeek.SUNDAY, LocalTime.of(2, 0), false, TimeDefinition.WALL, ZoneOffset.ofHours(-5), ZoneOffset.ofHours(-5), ZoneOffset.ofHours(-4)); System.out.println("Transition rule: " + rule); } }
此示例使用 WALL 时间定义创建转换规则。 该规则指定了 3 月第二个星期日凌晨 2 点的夏令时转换。 时间在转换期间被解释为挂钟时间。
比较时间定义
不同的时间定义可以为相同的本地时间产生不同的结果。 此示例比较了具有相同输入参数的所有三个定义。
import java.time.*; import java.time.zone.*; public class Main { public static void main(String[] args) { LocalDateTime localTime = LocalDateTime.of(2025, 11, 3, 1, 30); ZoneOffset standardOffset = ZoneOffset.ofHours(-5); ZoneOffset wallOffset = ZoneOffset.ofHours(-4); System.out.println("UTC: " + TimeDefinition.UTC.createDateTime(localTime, standardOffset, wallOffset)); System.out.println("WALL: " + TimeDefinition.WALL.createDateTime(localTime, standardOffset, wallOffset)); System.out.println("STANDARD: " + TimeDefinition.STANDARD.createDateTime(localTime, standardOffset, wallOffset)); } }
此示例清楚地显示了每个时间定义如何以不同的方式解释相同的本地时间。 UTC 将其视为 UTC 时间,WALL 应用挂钟偏移量,STANDARD 使用标准偏移量。
真实世界的转换示例
此示例演示了使用 WALL
时间定义的真实世界夏令时转换,这对于大多数时区来说是典型的。
import java.time.*; import java.time.zone.*; public class Main { public static void main(String[] args) { // New York DST transition in 2025 (spring forward) ZoneOffsetTransitionRule springRule = ZoneOffsetTransitionRule.of( Month.MARCH, 10, DayOfWeek.SUNDAY, LocalTime.of(2, 0), false, TimeDefinition.WALL, ZoneOffset.ofHours(-5), ZoneOffset.ofHours(-5), ZoneOffset.ofHours(-4)); // Apply the transition LocalDateTime beforeTransition = LocalDateTime.of(2025, 3, 10, 1, 59); LocalDateTime transitionTime = LocalDateTime.of(2025, 3, 10, 2, 0); LocalDateTime afterTransition = LocalDateTime.of(2025, 3, 10, 3, 0); System.out.println("Before: " + springRule.createDateTime(beforeTransition, ZoneOffset.ofHours(-5), ZoneOffset.ofHours(-5))); System.out.println("At transition: " + springRule.createDateTime(transitionTime, ZoneOffset.ofHours(-5), ZoneOffset.ofHours(-4))); System.out.println("After: " + springRule.createDateTime(afterTransition, ZoneOffset.ofHours(-5), ZoneOffset.ofHours(-4))); } }
此示例模拟了纽约的夏令时转换。 在凌晨 2 点的挂钟时间,偏移量从 -5 小时变为 -4 小时。 WALL 定义确保转换在正确的本地时间发生。
来源
在本文中,我们介绍了 TimeDefinition 枚举及其在时区转换中的作用。 了解这些概念对于在 Java 应用程序中使用时区和夏令时更改至关重要。
作者
列出所有Java教程。