将ZonedDateTime转换为一天的结束

在此代码中,

Instant i = Instant.ofEpochMilli(inputDate);
ZonedDateTime zonedDateTime = ZonedDateTime.ofinstant(instance,ZoneId.of(marketplaceIdToZoneMap.get(timeZone)));

我只希望这个时区DateTime是一天的结束,

如果zonedDateTime的值是:2019-11-14 12:00:99

输出应为2019-11-14 23:59:59

abduaini83 回答:将ZonedDateTime转换为一天的结束

如果需要一天的最后一秒,可以使用:

ZonedDateTime eod = zonedDateTime.with(LocalTime.of(23,59,59));

如果您需要一天中的最长时间,可以使用:

ZonedDateTime eod = zonedDateTime.with(LocalTime.MAX);

请注意,在某些奇怪的情况下,23:59:59可能不是特定日期(通常是具有复杂时区更改的历史日期)的有效时间。

,

一个简单的解决方案是手动设置所需的值:

zonedDateTime.withHour(23).withMinute(59).withSecond(59);

另一种解决方案可能是重置小时/分钟/秒,添加一天并删除一秒钟:

zonedDateTime.truncatedTo(ChronoUnit.DAYS).plusDay(1).minusSecond(1);
本文链接:https://www.f2er.com/3095473.html

大家都在问