📌  相关文章
📜  Java的.time.zone.ZoneOffsetTransition类在Java中

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

Java的.time.zone.ZoneOffsetTransition类在Java中

两个偏移量之间的过渡通常是夏令时转换的结果,不连续性通常是春季的间隙和秋季的重叠,而这又是由本地时间线的不连续性引起的。

ZoneOffsetTransition 的函数是对这两个偏移量之间的过渡进行建模。

注意:该类是不可变的和线程安全的

方法:

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.

下面是 ZoneOffsetTransition 类的Java实现:

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);
    }
}


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