ZoneOffset 是 Java 中的一個類,表示相對於時區 UTC 的固定時區偏移。該類別繼承ZoneId類,並實作Comparable介面。
該類別聲明了三個欄位:
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
讓我們在以下部分中了解有關此類的更多資訊。
文法
下面是 ZoneOffset 類別的聲明
public final class ZoneOffset extends ZoneId implements TemporalAccessor, TemporalAdjuster, Comparable<ZoneOffset>, Serializable
以下是執行不同功能的 ZoneOffset 類別的不同方法:
所提及的時間物件 temp 將會被調整,以便與該物件具有相同的偏移量。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZonedDateTime dt = ZonedDateTime.now(); System.out.println(dt); ZoneOffset off = ZoneOffset.of("Z"); dt = (ZonedDateTime)off.adjustInto(dt); System.out.println(dt); } }
輸出:
此偏移量會以降序與另一個偏移量進行比較。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.of("Z"); ZoneOffset off2 = ZoneOffset.of("+03:05"); System.out.println("First offset :"+ off1); System.out.println("Second offset :"+ off2); int cmp = off1.compareTo(off2); System.out.println(cmp > 1 ? "Offset 1 is greater" : "Offset 2 is greater"); } }
輸出:
將此偏移量與另一個偏移量檢查它們是否相等。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.of("Z"); ZoneOffset off2 = ZoneOffset.of("+03:05"); System.out.println("First offset :"+ off1); System.out.println("Second offset :"+ off2); boolean cmp = off1.equals(off2); System.out.println(cmp); } }
輸出:
offset 實例來自時間物件。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.from(ZonedDateTime.now()); System.out.println(off1); } }
輸出:
欄位 f 的值將從偏移量中以 int 形式檢索。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.from(ZonedDateTime.now()); System.out.println(off1); System.out.println(off1.get(ChronoField.OFFSET_SECONDS)); } }
輸出:
將檢索標準化區域的偏移 ID。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.of("+03:50"); System.out.println(off1); System.out.println(off1.getId()); } }
輸出:
將從偏移量中檢索欄位 f 的值。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.from(ZonedDateTime.now()); System.out.println(off1); System.out.println(off1.getLong(ChronoField.OFFSET_SECONDS)); } }
輸出:
將檢索關聯時區的規則。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.from(ZonedDateTime.now()); System.out.println(off1); System.out.println(off1.getRules()); } }
輸出:
總區域偏移量將在幾秒鐘內檢索。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.from(ZonedDateTime.now()); System.out.println(off1); System.out.println(off1.getTotalSeconds()); } }
輸出:
zoneoffset 的實例將在數小時內檢索。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.ofHours(3); System.out.println(off1); } }
輸出:
zoneoffset 的實例將在幾小時和幾分鐘內檢索。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.ofHoursMinutes(3, 5); System.out.println(off1); } }
輸出:
zoneoffset 的實例將在小時、分鐘和秒內檢索。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.ofHoursMinutesSeconds(3, 5, 7); System.out.println(off1); } }
輸出:
將檢索 zoneoffset 的雜湊碼。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.from(ZonedDateTime.now()); System.out.println(off1); System.out.println(off1.hashCode()); } }
輸出:
zoneoffset 的實例將在幾秒鐘內檢索。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.ofTotalSeconds(2800); System.out.println(off1); } }
輸出:
檢查是否支援提到的欄位。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.of("Z"); System.out.println(off1); System.out.println(off1.isSupported(ChronoField.OFFSET_SECONDS)); } }
輸出:
將使用 ID 檢索 zoneoffset 的實例。
代碼:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.of("Z"); System.out.println(off1); } }
Output:
zoneoffset will be retrieved s string using an ID that is normalized.
Code:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { String off1 = ZoneOffset.of("Z").toString(); System.out.println(off1); } }
Output:
zoneoffset will be queried using the query mentioned.
Code:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.of("+08:00"); System.out.println(off1); System.out.println(off1.query(TemporalQueries.zone())); } }
Output:
The range of valid values will be retrieved for the field mentioned.
Code:
import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.temporal.ChronoField; import java.time.temporal.TemporalQueries; public class OffsetZoneEx { public static void main(String[] args) { ZoneOffset off1 = ZoneOffset.of("+08:00"); System.out.println(off1); System.out.println(off1.range(ChronoField.OFFSET_SECONDS )); } }
Output:
In Java, ZoneOffset is a class that denotes the fixed zone offset from the time zone UTC. MAX, MIN, and UTC are the three fields of this class. In this article, different details such as declaration, methods, and examples of Java ZoneOffset class is explained in detail.
以上是Java 區域偏移量的詳細內容。更多資訊請關注PHP中文網其他相關文章!