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

2

6

I have an Instant Date, eg: 2020-03-09T20:13:57.089Z and I want to find the end of next day in Instant, eg - 2020-03-10T23:59:59.089Z (this would be the end of next day compared to the initial date)

How do I do this using Instant in Java?

Elector answered 9/3, 2020 at 20:19 Comment(6)
Yes, use java.time, the modern Java date and time API, for your date and time work. And no, don’t represent the end of the day as 23:59:59-dot-something. Represent it as the first moment of the next day. It’s much simpler and also more correct. Only always remember that the day lasts until that moment exclusive.Jog
Where does the 0.089 seconds come from? Or do you just want 23:59:59 exactly?Rowen
1) You say "midnight" and you say 11:59:59.089Z, which is noon, so which is it? --- 2) Is it a requirement to retain the .089 milliseconds like you showed? If so, you should explicitly say so, to clarify your requirements.Radiotelephone
"2020-03-10T11:59:59.089Z (this would be the end of next day)" I have never thought of noon (aka "midday") as being the "end of day" for anything. Are you sure you didn't mean 23:59:59?Radiotelephone
The title was revised by another user. I meant the end of next day, that's 23:59:59. I re-edited the title to match this. @RadiotelephoneElector
Ok, so for the third time: Sample shown time is 11:59:59, which is noon, but you ask for "end of day", aka midnight, which is 23:59:59. Please fix the discrepancy in your question text, i.e. change 11 to 23, OR change "end of day" / "midnight" to "middle of day" / "noon".Radiotelephone
C
3

One way is converting Instant to ZonedDateTime with UTC timezone and modify the date as per requirement and then convert it back

Midday :

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

End of day :

Instant result = instant.atOffset(ZoneOffset.UTC)
                        .plusDays(1).with(LocalTime.of(23,59,59,instant.getNano()))
                        .toInstant();
Cipango answered 9/3, 2020 at 20:29 Comment(3)
try my updated code it gives exactly what you need for @YashDamaniCipango
Great answer. --- But, you should use atOffset() instead of atZone().Radiotelephone
Yes, I tried it. I made an error in my question which is my bad. Nevertheless, I updated my question and adjusted the solution accordingly. Thanks! @DeadpoolElector
C
7

If you want the actual midnight (00:00), you can use:

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

Otherwise, another solution would be:

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();
Catania answered 9/3, 2020 at 20:26 Comment(5)
no, I don't want the actual midnight, but the next day's (11:59). I'm sorry, should've been more clear with the question.Elector
Instead of ldt1.withHour(23).withMinute(59).withSecond(59), I think ldt1.toLocalDate().atTime(23, 59, 59) would be better, because it resets fractional seconds to 0, and it has fewer intermediate objects.Radiotelephone
Thanks! I added 2 days and subtracted a second like you mentioned, and it worked. :) @RadiotelephoneElector
The conversion to/from LocalDateTime must be done with UTC time zone, otherwise the resulting time will not be 23:59:59ZRadiotelephone
That's true! Since Instant objects deal with UTC by default, converting it using Instant functions worked as well. @RadiotelephoneElector
C
3

One way is converting Instant to ZonedDateTime with UTC timezone and modify the date as per requirement and then convert it back

Midday :

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

End of day :

Instant result = instant.atOffset(ZoneOffset.UTC)
                        .plusDays(1).with(LocalTime.of(23,59,59,instant.getNano()))
                        .toInstant();
Cipango answered 9/3, 2020 at 20:29 Comment(3)
try my updated code it gives exactly what you need for @YashDamaniCipango
Great answer. --- But, you should use atOffset() instead of atZone().Radiotelephone
Yes, I tried it. I made an error in my question which is my bad. Nevertheless, I updated my question and adjusted the solution accordingly. Thanks! @DeadpoolElector

© 2022 - 2024 — McMap. All rights reserved.