Home > Java > javaTutorial > Java ZoneOffset

Java ZoneOffset

王林
Release: 2024-08-30 15:50:35
Original
620 people have browsed it

ZoneOffset is a class in Java that denotes the fixed zone offset from the time zone UTC. This class inherits the class ZoneId and implements the interface Comparable.

This class states three fields:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • MAX:  Maximum supported offset, which is a constant
  • MIN: Minimum supported offset, which is a constant
  • UTC: Offset constant of the time zone for UTC (ID: ‘Z’)

Let us see more about this class in the following sections.

Syntax

Below is the declaration for ZoneOffset class

public final class ZoneOffset extends ZoneId implements TemporalAccessor, TemporalAdjuster, Comparable<ZoneOffset>, Serializable
Copy after login

Methods of Java ZoneOffset

The following are the different methods for the ZoneOffset class that performs different functionalities:

1. adjustInto(Temporaltemp)

The temporal object temp mentioned will be adjusted in order to have offset same as this object.

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) {
ZonedDateTime dt = ZonedDateTime.now();
System.out.println(dt);
ZoneOffset off = ZoneOffset.of("Z");
dt = (ZonedDateTime)off.adjustInto(dt);
System.out.println(dt);
}
}
Copy after login

Output:

Java ZoneOffset

2. compareTo(ZoneOffsetotheroff)

This offset is compared with another one in descending order.

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("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");
} }
Copy after login

Output:

Java ZoneOffset

3. equals(Objectobj)

This offset is checked with another one whether they are equal.

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("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);    } }
Copy after login

Output:

Java ZoneOffset

4. from(TemporalAccessortemp)

An instance of offset is from a temporal object.

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.from(ZonedDateTime.now());
System.out.println(off1);
}
}
Copy after login

Output:

Java ZoneOffset

5. get(TemporalFieldf)

Value of field f will be retrieved from the offset as int.

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.from(ZonedDateTime.now());
System.out.println(off1);
System.out.println(off1.get(ChronoField.OFFSET_SECONDS));
} }
Copy after login

Output:

Java ZoneOffset

6. getId( )

THE offset ID of the normalized zone will be retrieved.

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("+03:50");
System.out.println(off1);
System.out.println(off1.getId());
}
}
Copy after login

Output:

Java ZoneOffset

7. getLong(TemporalFieldf)

Value of field f will be retrieved from the offset as long.

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.from(ZonedDateTime.now());
System.out.println(off1);
System.out.println(off1.getLong(ChronoField.OFFSET_SECONDS));
} }
Copy after login

Output:

Java ZoneOffset

8. getRules()

Rules will be retrieved for the associated time zone.

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.from(ZonedDateTime.now());
System.out.println(off1);
System.out.println(off1.getRules());
} }
Copy after login

Output:

Java ZoneOffset

9. getTotalSeconds( )

Total zone offset will be retrieved in seconds.

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.from(ZonedDateTime.now());
System.out.println(off1);
System.out.println(off1.getTotalSeconds()); } }
Copy after login

Output:

Java ZoneOffset

10. ofHours(int hrs)

An instance of zoneoffset will be retrieved in hours.

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.ofHours(3);
System.out.println(off1);
} }
Copy after login

Output:

Java ZoneOffset

11. ofHoursMinutes(int hrs, int mints)

An instance of zoneoffset will be retrieved in hours and minutes.

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.ofHoursMinutes(3, 5);
System.out.println(off1);   } }
Copy after login

Output:

Java ZoneOffset

12. ofHoursMinutesSeconds(int hrs, int mints, int sec)

An instance of zoneoffset will be retrieved in hours, minutes and seconds.

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.ofHoursMinutesSeconds(3, 5, 7);
System.out.println(off1);
} }
Copy after login

Output:

Java ZoneOffset

13. hashCode()

The hashcode of the zoneoffset will be retrieved.

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.from(ZonedDateTime.now());
System.out.println(off1);
System.out.println(off1.hashCode());
} }
Copy after login

Output:

Java ZoneOffset

14. ofTotalSeconds(int totalsecs)

An instance of zoneoffset will be retrieved in seconds.

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.ofTotalSeconds(2800);
System.out.println(off1);
} }
Copy after login

Output:

Java ZoneOffset

15. isSupported(TemporalFieldf)

Checks whether the mentioned field is supported.

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("Z");
System.out.println(off1);
System.out.println(off1.isSupported(ChronoField.OFFSET_SECONDS));
} }
Copy after login

Output:

Java ZoneOffset

16. of(Stringoffsetid)

An instance of zoneoffset will be retrieved using ID.

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("Z");
System.out.println(off1);
} }
Copy after login

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);
} }
Copy after login

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()));  } }
Copy after login

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 ));
} }
Copy after login

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.

The above is the detailed content of Java ZoneOffset. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template