Java OffsetTime 类
最后修改时间:2025 年 4 月 16 日
java.time.OffsetTime 类表示带 UTC/格林威治时区偏移量的时间。它存储时间到纳秒精度,并带有时区偏移量。这对于表示不同时区的时间很有用。
OffsetTime 是不可变的,并且是线程安全的。它结合了 LocalTime 和 ZoneOffset。该类非常适合需要记录带有 UTC 偏移量但没有完整时区 ID 的应用程序。
OffsetTime 类概述
OffsetTime 提供了创建、解析和操作带有偏移量的时间的方法。关键操作包括时间比较、时间算术和格式化。该类处理的时间精度高达纳秒。
public final class OffsetTime implements Temporal, TemporalAdjuster,
Comparable<OffsetTime>, Serializable {
public static OffsetTime now();
public static OffsetTime now(ZoneId zone);
public static OffsetTime of(LocalTime time, ZoneOffset offset);
public static OffsetTime parse(CharSequence text);
public LocalTime toLocalTime();
public ZoneOffset getOffset();
public int getHour();
public int getMinute();
public int getSecond();
public int getNano();
public boolean isBefore(OffsetTime other);
public boolean isAfter(OffsetTime other);
public OffsetTime plusHours(long hours);
public OffsetTime minusMinutes(long minutes);
}
上面的代码展示了 OffsetTime 提供的主要方法。这些方法允许创建、比较和操作带有偏移量的时间。该类提供高达纳秒的精度,同时保持时区感知。
创建 OffsetTime 对象
OffsetTime 对象可以通过多种方式创建。最常用的方法是 now 用于当前时间,以及用于特定时间的工厂方法。也支持从字符串解析。
package com.zetcode;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
// Current offset time
OffsetTime now = OffsetTime.now();
System.out.println("Current offset time: " + now);
// From LocalTime and ZoneOffset
OffsetTime specific = OffsetTime.of(
LocalTime.of(14, 30),
ZoneOffset.ofHours(2)
);
System.out.println("Specific offset time: " + specific);
// From string
OffsetTime parsed = OffsetTime.parse("10:15:30+01:00");
System.out.println("Parsed from string: " + parsed);
// With specific zone
OffsetTime inZone = OffsetTime.now(ZoneOffset.of("-05:00"));
System.out.println("Time in -05:00 offset: " + inZone);
}
}
此示例演示了创建 OffsetTime 对象的不同方法。输出显示了带有其偏移量的 ISO-8601 格式的时间。 now 方法在可用时使用系统默认偏移量。
获取 OffsetTime 组件
OffsetTime 可以分解为其时间组件和偏移量。这些值表示本地时间和其 UTC 偏移量。这些方法对于单独显示或处理时间部分很有用。
package com.zetcode;
import java.time.OffsetTime;
public class Main {
public static void main(String[] args) {
OffsetTime offsetTime = OffsetTime.parse("15:45:30.123456789+02:00");
// Get time components
System.out.println("Hour: " + offsetTime.getHour());
System.out.println("Minute: " + offsetTime.getMinute());
System.out.println("Second: " + offsetTime.getSecond());
System.out.println("Nano: " + offsetTime.getNano());
// Get offset
System.out.println("Offset: " + offsetTime.getOffset());
// Get LocalTime part
System.out.println("Local time: " + offsetTime.toLocalTime());
}
}
此示例展示了如何从 OffsetTime 中提取组件。时间部分相对于本地时间,而不是 UTC。偏移量表示与 UTC/格林威治时间的差异。
比较 OffsetTime
可以比较 OffsetTime 以确定时间顺序。该类提供了 isBefore、isAfter 和 compareTo 方法。比较会考虑时间和偏移量组件。
package com.zetcode;
import java.time.OffsetTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
OffsetTime time1 = OffsetTime.parse("10:00:00+01:00");
OffsetTime time2 = OffsetTime.parse("11:00:00+02:00");
OffsetTime time3 = OffsetTime.parse("09:00:00+00:00");
System.out.println("time1 is before time2: " + time1.isBefore(time2));
System.out.println("time1 is after time3: " + time1.isAfter(time3));
System.out.println("Comparison time1 vs time2: " + time1.compareTo(time2));
// Equality check
OffsetTime sameTime = OffsetTime.of(time1.toLocalTime(), time1.getOffset());
System.out.println("time1 equals sameTime: " + time1.equals(sameTime));
}
}
此示例演示了比较 OffsetTime 对象的各种方法。比较方法考虑了本地时间和偏移量。请注意,相等性需要两个组件完全匹配。
添加和减去时间
OffsetTime 支持通过 plus 和 minus 方法进行时间算术运算。这些操作对于计算未来或过去的时间很有用。在这些操作期间,偏移量保持不变。
package com.zetcode;
import java.time.OffsetTime;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
OffsetTime now = OffsetTime.now();
// Add hours
OffsetTime inTwoHours = now.plusHours(2);
System.out.println("In two hours: " + inTwoHours);
// Subtract minutes
OffsetTime thirtyMinsAgo = now.minusMinutes(30);
System.out.println("Thirty minutes ago: " + thirtyMinsAgo);
// Add using ChronoUnit
OffsetTime in90Secs = now.plus(90, ChronoUnit.SECONDS);
System.out.println("In 90 seconds: " + in90Secs);
// Complex operation
OffsetTime modified = now.plusHours(3).minusMinutes(15);
System.out.println("Modified time: " + modified);
}
}
此示例展示了使用 OffsetTime 执行时间算术运算的各种方法。操作可以使用特定的单位方法或 ChronoUnit。所有计算都只影响时间组件,同时保留原始偏移量。
在时间类型之间转换
OffsetTime 可以与 LocalTime 等其他时间类型相互转换。当处理不同的时间表示时,这些转换是必不可少的。
package com.zetcode;
import java.time.OffsetTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
OffsetTime offsetTime = OffsetTime.parse("14:30:15+03:00");
// Convert to LocalTime
LocalTime localTime = offsetTime.toLocalTime();
System.out.println("Local time: " + localTime);
// Convert back to OffsetTime
OffsetTime back = OffsetTime.of(localTime, ZoneOffset.ofHours(2));
System.out.println("New offset time: " + back);
// With different offset
OffsetTime sameTimeDiffOffset = offsetTime.withOffsetSameLocal(
ZoneOffset.ofHours(-5)
);
System.out.println("Same local time, different offset: " +
sameTimeDiffOffset);
}
}
此示例演示了 OffsetTime 和其他时间类型之间的转换。请注意,转换为 LocalTime 会丢失偏移量信息。 withOffsetSameLocal 方法仅更改偏移量。
格式化和解析
OffsetTime 支持通过 DateTimeFormatter 进行格式化和解析。这允许自定义带有偏移量的时间的字符串表示。
package com.zetcode;
import java.time.OffsetTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
OffsetTime time = OffsetTime.parse("16:45:30+02:00");
// Predefined formatters
DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_OFFSET_TIME;
System.out.println("ISO format: " + isoFormatter.format(time));
// Custom formatter
DateTimeFormatter customFormatter = DateTimeFormatter
.ofPattern("hh:mm a xxx");
String formatted = customFormatter.format(time);
System.out.println("Custom format: " + formatted);
// Parsing with custom format
OffsetTime parsed = OffsetTime.parse("09:30 PM +0000",
DateTimeFormatter.ofPattern("hh:mm a xx"));
System.out.println("Parsed custom format: " + parsed);
}
}
此示例展示了如何格式化和解析 OffsetTime 对象。DateTimeFormatter 提供了基于模式的灵活格式化。预定义和自定义模式都支持解析和格式化。
来源
在本文中,我们介绍了 Java OffsetTime 类的基本方法和特性。理解这些概念对于在 Java 应用程序中准确处理带有偏移量的时间至关重要。
作者
列出所有Java教程。