Open In App

java.time.zone.ZoneOffsetTransition Class in Java

Last Updated : 12 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

A transition between two offsets is normally the result of a daylight savings cutover and the discontinuity is normally a gap in spring and an overlap in autumn and this, in turn, is caused by a discontinuity in the local timeline. 

ZoneOffsetTransition’s function is to model the transition between these two offsets.

Note: The class is immutable and thread-safe

Methods:

Methods 

Description

createTransition(int year)

This method creates a transition instance for the specified year.

of(LocalDateTime transition, ZoneOffset offsetBefore, ZoneOffset offsetAfter)

This is a static method used to retrieve the object of the transmission between the before and after offsets

equals()

This method is used to compare two ZoneOffsetTransition objects.

compareTo()

This method is used to compare two ZoneOffsetTransition objects

hashCode()

This method is used to retrieve the hash code of the current ZoneOffsetTransition object.

 getDateTimeAfter()

This method returns a LocalDateTime object with the after offset applied.

getDateTimeBefore()

This method returns a LocalDateTime object with the before offset applied.

getDuration()

This method returns the duration object of the transition.

getInstant()

This method returns an Instant object of the first instant when the afteroffset is applied.

 getOffsetAfter()

This method returns the ZoneOffset object after the transition.

getOffsetBefore()

This method returns the ZoneOffset object after the transition.

 isGap()

This method returns whether the current transition represents a gap in the local time-line.

 isOverlap()

This method returns whether the current transition represents an overlap in the local time-line.

 isValidOffset(ZoneOffset zoneOffset)

This method checks whether the ZoneOffset object passed is a valid one or not. i.e returns true If the ZoneOffset object is valid at atleast one point. The ZoneOffset object passed will be a valid one if an overlap exists at either the before offset or after the offset.

toEpochSecond()

This method returns the transition instant as an epoch second(long value)

toString()

This method returns a String object describing the current ZoneOffsetTransition object.

Below is the Java implementation of the ZoneOffsetTransition Class:

Java




import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.zone.*;
import java.util.*;
  
public class GeeksForGeeks {
    public static void main(String args[])
    {
  
        int year1 = 1996, year2 = 2001;
  
        // creating the offsets
        ZoneOffset ofSet1 = ZoneOffset.ofTotalSeconds(5);
        ZoneOffset ofSet2 = ZoneOffset.ofTotalSeconds(2);
  
        // creating two transition zones
        ZoneOffsetTransition z1 = ZoneOffsetTransition.of(
            LocalDateTime.of(year1, 11, 24, 03, 24, 48, 0),
            ofSet1, ofSet2);
        ZoneOffsetTransition z2 = ZoneOffsetTransition.of(
            LocalDateTime.of(year2, 10, 30, 01, 21, 52, 0),
            ofSet1, ofSet2);
  
        // checking for equality of
        // the two transistion zones.
        boolean ans = z1.equals(z2);
        if (ans)
            System.out.println(
                "The transition zones match!");
        else
            System.out.println(
                "Sorry, the transition zones do not match!");
  
        // using the getOffsetAfter to find the
        // offsetAfter of a transition zone
        ZoneOffset zone = z1.getOffsetAfter();
        System.out.println(
            "The offsetAfter of the first transition zone is"
            + zone);
    }
}


Output

Sorry, the transition zones do not match!
The offsetAfter of the first transition zone is+00:00:02


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads