如何从给定的 Instant 获取第二天结束日期时间? (Java)

How to get next day end of day date time from given Instant ? (Java)

我有一个 Instant Date,例如:2020-03-09T20:13:57.089Z 我想在 Instant 中找到第二天的结束时间,例如 - 2020-03-10T23:59:59.089Z(与初始日期)

如何在 Java 中使用 Instant 执行此操作?

如果你想要实际的午夜 (00:00),你可以使用:

instant.plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.DAYS);

否则,另一个解决方案是:

LocalDateTime ldt1 = LocalDateTime.ofInstant(instant.plus(1, ChronoUnit.DAYS), ZoneId.systemDefault());

ldt1 = ldt1
        .withHour(23)
        .withMinute(59)
        .withSecond(59);

Instant result = ldt1.atZone(ZoneId.systemDefault()).toInstant();

一种方法是使用 UTC 时区将 Instant 转换为 ZonedDateTime 并根据要求修改日期,然后将其转换回

中午:

Instant result = instant.atOffset(ZoneOffset.UTC)
                        .plusDays(1).with(LocalTime.of(11,59,59,instant.getNano()))
                        .toInstant();

一天结束:

Instant result = instant.atOffset(ZoneOffset.UTC)
                        .plusDays(1).with(LocalTime.of(23,59,59,instant.getNano()))
                        .toInstant();