ZonedDateTime withEarlierOffsetAtOverlap() method in Java with Examples

The withEarlierOffsetAtOverlap() method of a ZonedDateTime class is used to return a copy of this ZonedDateTime object after changing the zone offset to the earlier of the two valid offsets at a local time-line overlap. The overlap happens when Daylight saving time finishes and one hour is returned to timeline. Due to this there are two valid offsets for the local date-time and calling withEarlierOffsetAtOverlap method will return a ZonedDateTime with the earlier of the two zones. If this method is called when it is not an overlap, this is returned. This instance is immutable and unaffected by this method call.

Syntax:

public ZonedDateTime withEarlierOffsetAtOverlap()

Parameters: This method accepts no parameters.



Return value: This method returns a ZonedDateTime based on this date-time with the earlier offset.

Below programs illustrate the withEarlierOffsetAtOverlap() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// withEarlierOffsetAtOverlap() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ZonedDateTime object
        ZonedDateTime zonedDT
            = ZonedDateTime
                  .ofLocal(
                      LocalDateTime.of(2018, 11, 4, 1, 25, 43),
                      ZoneId.of("US/Central"),
                      ZoneOffset.ofHours(-6));
  
        // print ZonedDateTime
        System.out.println("Before"
                           + " withEarlierOffsetAtOverlap():\n "
                           + zonedDT);
  
        // apply withEarlierOffsetAtOverlap()
        ZonedDateTime zonedDT2
            = zonedDT.withEarlierOffsetAtOverlap();
  
        // print ZonedDateTime after
        // withEarlierOffsetAtOverlap()
        System.out.println("\nAfter"
                           + " withEarlierOffsetAtOverlap():\n "
                           + zonedDT2);
    }
}

chevron_right


Output:

Before withEarlierOffsetAtOverlap():
 2018-11-04T01:25:43-06:00[US/Central]

After withEarlierOffsetAtOverlap():
 2018-11-04T01:25:43-05:00[US/Central]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// withEarlierOffsetAtOverlap() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ZonedDateTime object
        ZonedDateTime zonedDT
            = ZonedDateTime
                  .ofLocal(
                      LocalDateTime.of(2021, 11, 07, 1, 05, 53),
                      ZoneId.of("US/Central"),
                      ZoneOffset.ofHours(-6));
  
        // print ZonedDateTime
        System.out.println("Before"
                           + " withEarlierOffsetAtOverlap():\n "
                           + zonedDT);
  
        // apply withEarlierOffsetAtOverlap()
        ZonedDateTime zonedDT2
            = zonedDT.withEarlierOffsetAtOverlap();
  
        // print ZonedDateTime after
        // withEarlierOffsetAtOverlap()
        System.out.println("\nAfter"
                           + " withEarlierOffsetAtOverlap():\n "
                           + zonedDT2);
    }
}

chevron_right


Output:

Before withEarlierOffsetAtOverlap():
 2021-11-07T01:05:53-06:00[US/Central]

After withEarlierOffsetAtOverlap():
 2021-11-07T01:05:53-05:00[US/Central]

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#withEarlierOffsetAtOverlap()



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.