📜  Java的.time.ZoneOffset类在Java中

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

Java的.time.ZoneOffset类在Java中

时区偏移量是时区与格林威治/UTC 不同的时间量。这通常是一个艰难而快速的小时数和分钟数。从Java的time包中,ZoneOffset类用于表示UTC区域的固定区域偏移量,继承了ZoneId类,实现了Comparable接口。

ZoneOffset 类具有三个字段:

  • MAX:支持的最大区域偏移量。
  • MIN:它是支持的最小区域偏移量。
  • UTC:它是 UTC 的时区偏移常数。

ZoneOffset 类的方法



MethodDescription
adjustInto(Temporal temporal)This method is used to adjusts the specified temporal object to have the same offset as this object.
compareTo(ZoneOffset other) This method compares this offset to another offset in descending order.
equals(Object obj) This method checks if this offset is equal to another offset.
from(TemporalAccessor temporal) This method obtains an instance of ZoneOffset from a temporal object.
get(TemporalField field)This method is Used to get the value of the specified field from this offset as an int
getId() This method gets the normalized zone offset ID.
getLong(TemporalField field) This method gets the value of the specified field from this offset as a long.
getRules()This method gets the associated time-zone rules.
getTotalSeconds() This method gets the total zone offset in seconds.
hashCode() A hash code for this offset.
isSupported(TemporalField field)This method checks if the specified field is supported.
of(String offsetId)This method is Used to obtain an instance of ZoneOffset using the ID.
ofHours(int hours)This method is Used to obtain an instance of ZoneOffset using an offset in hours
ofHoursMinutes(int hours, int minutes)This method is Used to obtain an instance of ZoneOffset using an offset in hours and minute
ofHoursMinutesSeconds(int hours, int minutes, int seconds)This method obtains an instance of ZoneOffset using an offset in hours, minutes and seconds.
ofTotalSeconds(int totalSeconds)This method obtains an instance of ZoneOffset specifying the total offset in seconds
query(TemporalQuery query)This method queries this offset using the specified query.
range(TemporalField field) This method gets the range of valid values for the specified field.
toString() This method outputs this offset as a String, using the normalized ID.

1. ofHoursMinutes() 方法:

此方法用于使用以小时和分钟为单位的偏移量来获取 ZoneOffset 的实例。

Java
// Example of ofHoursMinutes() Method
import java.time.ZoneOffset; 
public class GFG { 
  public static void main(String[] args) { 
    ZoneOffset zone = ZoneOffset.ofHoursMinutes(7,15); 
    System.out.println(zone); 
  } 
}


Java
//Example of ofHours() Method
import java.time.*;
import java.time.ZoneOffset; 
public class GFG { 
  public static void main(String[] args) { 
    ZoneOffset zone = ZoneOffset.ofHours(4); 
    System.out.println(zone); 
  } 
}


输出
+07:15

2. ofHours() 方法

使用以小时为单位的偏移量获取 ZoneOffset 的实例。

Java

//Example of ofHours() Method
import java.time.*;
import java.time.ZoneOffset; 
public class GFG { 
  public static void main(String[] args) { 
    ZoneOffset zone = ZoneOffset.ofHours(4); 
    System.out.println(zone); 
  } 
}
输出
+04:00