前言
instant
是 java 8 引入的日期时间 api 中的一个核心类,用于表示 时间线上的一个瞬时点(以 unix 纪元 1970-01-01t00:00:00z 为起点)。它是不可变的(线程安全),适用于记录时间戳、性能分析、跨时区事件处理等场景。以下是关于instant
的详细介绍:
一、核心特性
- 不可变性
- 所有操作(如加减、调整)都会返回新对象,原对象保持不变。
- 线程安全
- 由于不可变性,无需同步即可安全使用。
- 高精度
- 支持纳秒级精度(
getnano()
方法)。
- 支持纳秒级精度(
- utc 时间
- 默认以 utc 时区表示时间,不包含时区信息。
- 与传统类的兼容性
- 可与
date
、localdatetime
、zoneddatetime
等类互转。
- 可与
二、创建instant实例
1.now():获取当前时间的instant
instant now = instant.now(); system.out.println("当前时间: " + now); // 输出如 2025-05-25t14:34:32.123456789z
2.ofepochsecond(long epochsecond):通过秒数创建
instant fromseconds = instant.ofepochsecond(1716549272); system.out.println("从秒数创建: " + fromseconds); // 对应 2025-05-25t14:34:32z
3.ofepochmilli(long epochmilli):通过毫秒数创建
instant frommillis = instant.ofepochmilli(1716549272123l); system.out.println("从毫秒数创建: " + frommillis); // 对应 2025-05-25t14:34:32.123z
4.parse(charsequence text):解析字符串为instant
instant parsed = instant.parse("2025-05-25t14:34:32.123z"); system.out.println("解析后的时间: " + parsed); // 输出相同时间
三、获取时间戳属性
1. 获取秒数
long seconds = now.getepochsecond(); // 获取自1970-01-01t00:00:00z以来的秒数 system.out.println("秒数: " + seconds);
2. 获取毫秒数
long millis = now.toepochmilli(); // 获取自1970-01-01t00:00:00z以来的毫秒数 system.out.println("毫秒数: " + millis);
3. 获取纳秒数
int nanos = now.getnano(); // 获取秒内的纳秒部分(0~999,999,999) system.out.println("纳秒数: " + nanos);
四、时间加减操作
1.plusseconds(long seconds)/minusseconds(long seconds)
instant plus10seconds = now.plusseconds(10); // 当前时间 + 10秒 instant minus5seconds = now.minusseconds(5); // 当前时间 - 5秒 system.out.println("加10秒: " + plus10seconds); system.out.println("减5秒: " + minus5seconds);
2.plus(duration duration)/minus(duration duration)
duration duration = duration.ofhours(2); // 2小时 instant plus2hours = now.plus(duration); instant minus2hours = now.minus(duration); system.out.println("加2小时: " + plus2hours); system.out.println("减2小时: " + minus2hours);
五、与其他时间类的转换
1. 转换为localdatetime(需指定时区)
localdatetime localdatetime = now.atzone(zoneid.systemdefault()).tolocaldatetime(); system.out.println("本地时间: " + localdatetime);
2. 转换为zoneddatetime
zoneddatetime zoneddatetime = now.atzone(zoneid.of("asia/shanghai")); system.out.println("带时区的时间: " + zoneddatetime); // 输出 2025-05-25t22:34:32.123+08:00[asia/shanghai]
3. 转换为date
date date = date.from(now); system.out.println("转换为date: " + date);
4. 从date转换为instant
instant fromdate = date.toinstant(); system.out.println("从date转换: " + fromdate);
六、时间比较
1.isbefore(instant other)/isafter(instant other)
instant future = now.plusseconds(100); boolean isbefore = now.isbefore(future); // true system.out.println("是否在目标时间之前: " + isbefore);
2.equals(instant other):判断是否相等
instant sameinstant = now; boolean isequal = now.equals(sameinstant); // true system.out.println("是否相等: " + isequal);
七、特殊常量与边界值
1.epoch:unix 纪元起点
instant epoch = instant.epoch; system.out.println("纪元起点: " + epoch); // 1970-01-01t00:00:00z
2.min/max:时间范围边界
instant mininstant = instant.min; // -10^6 - 1970-01-01t00:00:00z instant maxinstant = instant.max; // +10^6 - 1970-01-01t00:00:00z system.out.println("最小时间: " + mininstant); system.out.println("最大时间: " + maxinstant);
八、格式化与解析
1. 格式化为字符串
string formatted = now.tostring(); // 默认格式:2025-05-25t14:34:32.123456789z system.out.println("格式化后的时间: " + formatted);
2. 自定义格式解析
datetimeformatter formatter = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss xxx"); instant customparsed = instant.parse("2025-05-25 14:34:32 +0800", formatter); system.out.println("自定义解析后的时间: " + customparsed); // 输出 utc 时间
九、常见用例示例
1. 记录代码执行时间
instant start = instant.now(); // 执行耗时操作 thread.sleep(1000); instant end = instant.now(); duration duration = duration.between(start, end); system.out.println("耗时: " + duration.toseconds() + " 秒");
2. 跨时区时间转换
instant utctime = instant.now(); zoneddatetime newyorktime = utctime.atzone(zoneid.of("america/new_york")); system.out.println("纽约时间: " + newyorktime); // 输出如 2025-05-25t08:34:32.123-04:00[america/new_york]
3. 处理历史时间戳
instant historicalevent = instant.ofepochsecond(-123456); // 1970年之前的事件 system.out.println("历史事件时间: " + historicalevent); // 输出如 -0001-12-01t00:00:00z
十、完整代码示例
import java.time.*; import java.time.format.datetimeformatter; public class instantexample { public static void main(string[] args) { // 创建instant instant now = instant.now(); system.out.println("当前时间: " + now); // 获取时间戳 long seconds = now.getepochsecond(); long millis = now.toepochmilli(); int nanos = now.getnano(); system.out.println("秒数: " + seconds + ", 毫秒数: " + millis + ", 纳秒数: " + nanos); // 时间加减 instant plus10sec = now.plusseconds(10); instant minus5sec = now.minusseconds(5); system.out.println("加10秒: " + plus10sec); system.out.println("减5秒: " + minus5sec); // 转换为其他时间类 zoneddatetime zoned = now.atzone(zoneid.of("asia/shanghai")); localdatetime local = zoned.tolocaldatetime(); system.out.println("本地时间: " + local); system.out.println("带时区的时间: " + zoned); // 时间比较 instant future = now.plusseconds(100); system.out.println("是否在目标时间之前: " + now.isbefore(future)); // 格式化与解析 string formatted = now.tostring(); system.out.println("格式化后的时间: " + formatted); datetimeformatter formatter = datetimeformatter.ofpattern("yyyy-mm-dd hh:mm:ss xxx"); instant parsed = instant.parse("2025-05-25 14:34:32 +0800", formatter); system.out.println("解析后的时间: " + parsed); } }
十一、总结
方法 | 用途 |
---|---|
now() / ofepochsecond() / ofepochmilli() / parse() | 创建 instant 实例 |
getepochsecond() / toepochmilli() / getnano() | 获取时间戳属性 |
plusseconds() / minusseconds() / plus() / minus() | 时间加减操作 |
atzone() / tolocaldatetime() / from(date) | 转换为其他时间类 |
isbefore() / isafter() / equals() | 时间比较 |
epoch / min / max | 特殊时间点 |
- 优势
- 高精度:支持纳秒级时间戳,适合高性能场景。
- 线程安全:不可变设计避免并发问题。
- 兼容性:与
date
、localdatetime
等无缝集成。 - 跨时区处理:通过
atzone()
自动适配时区。
到此这篇关于java中instant类使用的文章就介绍到这了,更多相关java instant类详解内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论