Rumah >Java >javaTutorial >Java ZoneOffset
ZoneOffset ialah kelas dalam Java yang menandakan zon tetap offset daripada zon waktu UTC. Kelas ini mewarisi kelas ZoneId dan melaksanakan antara muka Sebanding.
Kelas ini menyatakan tiga medan:
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Mari kita lihat lebih lanjut tentang kelas ini dalam bahagian berikut.
Sintaks
Di bawah ialah pengisytiharan untuk kelas ZoneOffset
public final class ZoneOffset extends ZoneId implements TemporalAccessor, TemporalAdjuster, Comparable<ZoneOffset>, Serializable
Berikut ialah kaedah berbeza untuk kelas ZoneOffset yang melaksanakan fungsi berbeza:
Suhu objek temporal yang dinyatakan akan dilaraskan untuk mengimbangi sama seperti objek ini.
Kod:
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); } }
Output:
Offset ini dibandingkan dengan yang lain dalam tertib menurun.
Kod:
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"); } }
Output:
Offset ini disemak dengan satu lagi sama ada ia sama.
Kod:
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); } }
Output:
Sebuah contoh ofset adalah daripada objek temporal.
Kod:
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); } }
Output:
Nilai medan f akan diambil dari offset sebagai int.
Kod:
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)); } }
Output:
ID offset bagi zon yang dinormalkan akan diambil semula.
Kod:
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()); } }
Output:
Nilai medan f akan diambil dari offset selama mana.
Kod:
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)); } }
Output:
Peraturan akan diambil untuk zon waktu yang berkaitan.
Kod:
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()); } }
Output:
Jumlah zon offset akan diambil dalam beberapa saat.
Kod:
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()); } }
Output:
Satu contoh zoneoffset akan diambil dalam beberapa jam.
Kod:
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); } }
Output:
Sebuah contoh zonoffset akan diambil dalam jam dan minit.
Kod:
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); } }
Output:
Satu kejadian zon offset akan diambil dalam jam, minit dan saat.
Kod:
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); } }
Output:
Kod cincang zonoffset akan diambil semula.
Kod:
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()); } }
Output:
Satu kejadian zonoffset akan diambil dalam beberapa saat.
Kod:
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); } }
Output:
Menyemak sama ada medan yang disebut disokong.
Kod:
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)); } }
Output:
Satu contoh zoneoffset akan diambil menggunakan ID.
Kod:
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.
Atas ialah kandungan terperinci Java ZoneOffset. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!