8

In this code,

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

I just want this time zoneDateTime to be end of the day,

like if value of zonedDateTime is : 2019-11-14 12:00:99

Output should come as 2019-11-14 23:59:59

assylias
  • 321,522
  • 82
  • 660
  • 783
Amit Kumar
  • 377
  • 4
  • 17
  • Not only has this question been asked before, it has also been answered with some very good answers. Please check the link to the original question. – Ole V.V. Nov 15 '19 at 20:23

2 Answers2

16

If you want the last second of the day, you can use:

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

If you want the maximum time of the day, you can use:

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

Note that there are weird situations where 23:59:59 may not be a valid time for a specific day (typically historical days with complicated timezone changes).

assylias
  • 321,522
  • 82
  • 660
  • 783
5

A simple solution is to manually set the values you want:

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

Another solution could be to reset the hours/minutes/second, add one day, and remove one second:

zonedDateTime.truncatedTo(ChronoUnit.DAYS).plusDay(1).minusSecond(1);
Fabien
  • 346
  • 2
  • 10