Rumah > Java > javaTutorial > Java ZoneOffset

Java ZoneOffset

王林
Lepaskan: 2024-08-30 15:50:35
asal
613 orang telah melayarinya

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

  • MAKS:  Offset maksimum yang disokong, iaitu pemalar
  • MIN: Offset minimum yang disokong, iaitu pemalar
  • UTC: Mengimbangi pemalar zon waktu untuk UTC (ID: ‘Z’)

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
Salin selepas log masuk

Kaedah Java ZoneOffset

Berikut ialah kaedah berbeza untuk kelas ZoneOffset yang melaksanakan fungsi berbeza:

1. laraskanKe(Temporaltemp)

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);
}
}
Salin selepas log masuk

Output:

Java ZoneOffset

2. compareTo(ZoneOffsetotheroff)

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");
} }
Salin selepas log masuk

Output:

Java ZoneOffset

3. sama dengan(Objectobj)

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);    } }
Salin selepas log masuk

Output:

Java ZoneOffset

4. daripada(TemporalAccessortemp)

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);
}
}
Salin selepas log masuk

Output:

Java ZoneOffset

5. dapatkan(TemporalFieldf)

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));
} }
Salin selepas log masuk

Output:

Java ZoneOffset

6. getId( )

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());
}
}
Salin selepas log masuk

Output:

Java ZoneOffset

7. getLong(TemporalFieldf)

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));
} }
Salin selepas log masuk

Output:

Java ZoneOffset

8. getRules()

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());
} }
Salin selepas log masuk

Output:

Java ZoneOffset

9. getTotalSeconds( )

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()); } }
Salin selepas log masuk

Output:

Java ZoneOffset

10. ofHours(int jam)

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);
} }
Salin selepas log masuk

Output:

Java ZoneOffset

11. ofHoursMinutes(int jam, int mint)

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);   } }
Salin selepas log masuk

Output:

Java ZoneOffset

12. ofHoursMinutesSeconds(int jam, int mint, int sec)

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);
} }
Salin selepas log masuk

Output:

Java ZoneOffset

13. Kod hash()

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());
} }
Salin selepas log masuk

Output:

Java ZoneOffset

14. ofTotalSeconds(int totalsecs)

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);
} }
Salin selepas log masuk

Output:

Java ZoneOffset

15. isSupported(TemporalFieldf)

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));
} }
Salin selepas log masuk

Output:

Java ZoneOffset

16. daripada(Stringoffsetid)

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);
} }
Salin selepas log masuk

Output:

Java ZoneOffset

17. toString()

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);
} }
Salin selepas log masuk

Output:

Java ZoneOffset

18. query(TemporalQuery query)

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()));  } }
Salin selepas log masuk

Output:

Java ZoneOffset

19. range(TemporalFieldf)

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 ));
} }
Salin selepas log masuk

Output:

Java ZoneOffset

Conclusion

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!

Label berkaitan:
sumber:php
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan