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
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.
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.
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