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中文网其他相关文章!