We have a library used for generating reports. It reads from a data file (SQL, XML, JSON, etc.), the datetime may then be modified in a user written equation, and then it is formatted as specified for the report output.
The use in an equation can be add a timespan, get parts of the value as in "if date.month == 2", and pretty much all the datetime macros in Excel.
Because the data can be JSON (or XML with no schema) the datetime can be "2019-01-25", "2019-01-25T14:32:23", "2019-01-25T14:32:23.12345", "2019-01-25T14:32:23Z", or "2019-01-25T14:32:23Z-0500" (last two can have the ".12345" also).
If there's no timezone offset we assume the datetime is UTC. While that should be true, often it isn't and it's local time but the way it's used, it doesn't matter. So making it UTC unless a timezone offset is specified works (up till now we've used Date).
First question - what class should I use to hold this value? From what I've read I think ZonedDateTime, but maybe Instant?
Second question - what class should I use for timespan when I have to do something like add 3 days to the datetime?
Third question - is there some parser that can parse all the different strings as I listed above? Or do I need to call String.contains() to determine the format and then do an explicit pattern based on that? And if so, using what class?
OffsetDateTime
would make more sense thanZonedDateTime
. – Ariannaarianne2019-01-25T14:32:23Z-0500
: do you need to know the offset of 5hrs? Or are you ok with only keeping the same instant in UTC, i.e.2019-01-25T19:32:23Z
? Or would you prefer having2019-01-25T14:32:23Z
(which is NOT the same instant but the same local time)? – Ariannaariannedatetime.plusDays(3)
to add 3 days. But there is no Timespan class in Java Time. You could easily write your own class or use ThreeTenInterval
class which is an extension of Java Time. – Ariannaarianne