Java ChronoZonedDateTime withEarlierOffsetAtOverlap()方法及示例
ChronoZonedDateTime 接口的 withEarlierOffsetAtOverlap() 方法是用来返回这个ChronoZonedDateTime对象的一个副本,在将区域偏移量改为本地时间线重叠处的两个有效偏移量中较早的一个。重叠发生在夏令时结束后,一个小时被返回到时间线上。由于这个原因,本地日期时间有两个有效的偏移量,调用withEarlierOffsetAtOverlap方法将返回一个ChronoZonedDateTime,它是两个区域中较早的。如果在不是重叠的时候调用这个方法,就会返回这个。这个实例是不可改变的,不受这个方法调用的影响。
语法
ChronoZonedDateTime withEarlierOffsetAtOverlap()
参数: 该方法不接受任何参数。
返回值: 该方法返回一个基于该日期时间的 ChronoZonedDateTime ,并带有较早的偏移。
以下程序说明了withEarlierOffsetAtOverlap()方法。
程序1 :
// Java program to demonstrate
// withEarlierOffsetAtOverlap() method
import java.time.*;
import java.time.chrono.*;
public class GFG {
public static void main(String[] args)
{
// create a ChronoZonedDateTime object
ChronoZonedDateTime zonedDT
= ZonedDateTime
.ofLocal(
LocalDateTime.of(2018, 11, 4, 1, 25, 43),
ZoneId.of("US/Central"),
ZoneOffset.ofHours(-6));
// print ChronoZonedDateTime
System.out.println("Before"
+ " withEarlierOffsetAtOverlap():\n "
+ zonedDT);
// apply withEarlierOffsetAtOverlap()
ChronoZonedDateTime zonedDT2
= zonedDT.withEarlierOffsetAtOverlap();
// print ChronoZonedDateTime after
// withEarlierOffsetAtOverlap()
System.out.println("\nAfter"
+ " withEarlierOffsetAtOverlap():\n "
+ zonedDT2);
}
}
输出:
Before withEarlierOffsetAtOverlap():
2018-11-04T01:25:43-06:00[US/Central]
After withEarlierOffsetAtOverlap():
2018-11-04T01:25:43-05:00[US/Central]
程序2
// Java program to demonstrate
// withEarlierOffsetAtOverlap() method
import java.time.*;
import java.time.chrono.*;
public class GFG {
public static void main(String[] args)
{
// create a ChronoZonedDateTime object
ChronoZonedDateTime zonedDT
= ZonedDateTime
.ofLocal(
LocalDateTime.of(2021, 11, 07, 1, 05, 53),
ZoneId.of("US/Central"),
ZoneOffset.ofHours(-6));
// print ChronoZonedDateTime
System.out.println("Before"
+ " withEarlierOffsetAtOverlap():\n "
+ zonedDT);
// apply withEarlierOffsetAtOverlap()
ChronoZonedDateTime zonedDT2
= zonedDT.withEarlierOffsetAtOverlap();
// print ChronoZonedDateTime after
// withEarlierOffsetAtOverlap()
System.out.println("\nAfter"
+ " withEarlierOffsetAtOverlap():\n "
+ zonedDT2);
}
}
输出:
Before withEarlierOffsetAtOverlap():
2021-11-07T01:05:53-06:00[US/Central]
After withEarlierOffsetAtOverlap():
2021-11-07T01:05:53-05:00[US/Central]
参考资料: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoZonedDateTime.html#withEarlierOffsetAtOverlap-