You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取当日时间为零点的Instant.now()实例?

获取当天UTC零点的Instant实例

要得到当前日期UTC时区零点的Instant,可以通过java.time包的类组合实现,步骤如下:

  1. 获取当前的Instant实例
  2. 将其转换为UTC时区的LocalDate,提取出当天的日期部分
  3. 把该LocalDate转换为UTC时区当天起始时刻的Instant

对应的Java代码示例:

Instant now = Instant.now();
// 转换为UTC时区的当天日期
LocalDate todayUtc = now.atZone(ZoneOffset.UTC).toLocalDate();
// 获取当天UTC零点的Instant
Instant startOfDayUtc = todayUtc.atStartOfDay(ZoneOffset.UTC).toInstant();

// 输出示例:2022-12-14T00:00:00Z
System.out.println(startOfDayUtc);

如果需要其他时区的当天零点,只需将ZoneOffset.UTC替换为目标时区,比如ZoneId.of("Asia/Shanghai")即可。

内容的提问来源于stack exchange,提问作者mmar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 03:25:17