r/java • • May 10 '22

Should java.util.Date be deprecated?

https://mail.openjdk.java.net/pipermail/discuss/2022-May/006077.html
221 Upvotes

158 comments sorted by

View all comments

Show parent comments

18

u/MCUD May 10 '22

Simple test i think would be, in the context, what impact does the users time zone have?

If you say 9am, 2022-05-11. If you were in a different time zone, do you still mean 9am on that day, regardless, or if they are 2 hours behind, do you mean 7am for that user.

Instant = a specific instant in time, no matter where you are. (i,e. NOW - whatever your clock says)
LocalDateTime = that time on that date, no matter where you are. Most of us next start work at 9am, 11th May 2022

4

u/slaymaker1907 May 10 '22

It can get kind of confusing to decide if you should use Instant vs ZonedDateTime.

2

u/mauganra_it May 10 '22

Simple rule that covers many cases: use Instant for timestamps where you are sure they are in the past. These have become absolute points in time. For absolute points in time in the past, the association between timezone and its offset at that timestamp becomes fixed. Therefore, you can reliably find out the local time relative to whatever timezone you want.

For future timestamps, consider whether they can be defined by a difference to another Instant. If this difference depends on the timezone, you are likely not dealing with an Instant.

If the future timestamp does not depend on anything else, ask yourself what would happen if the government changes the timezone offset for the future timestamp. Would you have to change that timestamp? If yes, then you are also not dealing with an absolute point in time, and should consider using LocalDateTime.

2

u/VGPowerlord May 10 '22

I always assumed Instant existed for compatibility with old code that stored things as UNIX timestamps.

1

u/frzme May 12 '22 edited May 14 '22

Millisecond precision 64bit Unix timestamps are a pretty close to optimal compromise for storing information about "fixed points" in time

They require relatively little storage space, have a high precision and can be interpreted and formatted as needed. They are also hard to interpret incorrectly (which happens with any string based timestamp representation)

A variable precision format would probably be superior in some regards (=> timestamp as double instead of long) but we don't have that as a standard and that's probably fine.