📌  相关文章
📜  Java中的 ChronoZonedDateTime withLaterOffsetAtOverlap() 方法及示例

📅  最后修改于: 2022-05-13 01:55:24.847000             🧑  作者: Mango

Java中的 ChronoZonedDateTime withLaterOffsetAtOverlap() 方法及示例

ChronoZonedDateTime接口的withLaterOffsetAtOverlap()方法用于在将区域偏移更改为本地时间线重叠处的两个有效偏移中的后者之后返回此 ChronoZonedDateTime 对象的副本。当夏令时结束并且一小时返回到时间线时,就会发生重叠。因此,本地日期时间有两个有效的偏移量,调用 withLaterOffsetAtOverlap 方法将返回一个 ChronoZonedDateTime 和两个区域中的后者。如果在不重叠时调用此方法,则返回 this。此实例是不可变的,不受此方法调用的影响。

句法:

ChronoZonedDateTime withLaterOffsetAtOverlap()

参数:此方法不接受任何参数。

返回值:此方法返回基于此日期时间的ChronoZonedDateTime以及较晚的偏移量。

下面的程序说明了 withLaterOffsetAtOverlap() 方法:

方案一:

// Java program to demonstrate
// ChronoZonedDateTime.withLaterOffsetAtOverlap() method
  
import java.time.*;
import java.time.chrono.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ChronoZonedDateTime object
        ChronoZonedDateTime zonedDT
            = ZonedDateTime.of(
                LocalDateTime.of(2018, 11, 4, 1, 25, 43),
                ZoneId.of("US/Central"));
  
        // print ChronoZonedDateTime
        System.out.println("Before withLaterOffsetAtOverlap(): "
                           + zonedDT);
  
        // apply withLaterOffsetAtOverlap()
        ChronoZonedDateTime zonedDT2
            = zonedDT.withLaterOffsetAtOverlap();
  
        // print ChronoZonedDateTime
        // after withLaterOffsetAtOverlap()
        System.out.println("After withLaterOffsetAtOverlap(): "
                           + zonedDT2);
    }
}
输出:
Before withLaterOffsetAtOverlap(): 2018-11-04T01:25:43-05:00[US/Central]
After withLaterOffsetAtOverlap(): 2018-11-04T01:25:43-06:00[US/Central]

方案二:

// Java program to demonstrate
// ChronoZonedDateTime.withLaterOffsetAtOverlap() method
  
import java.time.*;
import java.time.chrono.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ChronoZonedDateTime object
        ChronoZonedDateTime zonedDT
            = ZonedDateTime.of(
                LocalDateTime.of(2021, 11, 07, 1, 05, 53),
                ZoneId.of("US/Central"));
  
        // print ChronoZonedDateTime
        System.out.println("Before withLaterOffsetAtOverlap(): "
                           + zonedDT);
  
        // apply withLaterOffsetAtOverlap()
        ChronoZonedDateTime zonedDT2
            = zonedDT.withLaterOffsetAtOverlap();
  
        // print ChronoZonedDateTime
        // after withLaterOffsetAtOverlap()
        System.out.println("After withLaterOffsetAtOverlap(): "
                           + zonedDT2);
    }
}
输出:
Before withLaterOffsetAtOverlap(): 2021-11-07T01:05:53-05:00[US/Central]
After withLaterOffsetAtOverlap(): 2021-11-07T01:05:53-06:00[US/Central]

参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/chrono/ChronoZonedDateTime.html#withLaterOffsetAtOverlap–