What Java DateTime class should I use?
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?
I might be horrible wrong, but can you use DateTimeFormatter
with optional parts on pattern and parseBest method:
List<String> dates = List.of(
"2019-01-25",
"2019-01-25T14:32:23",
"2019-01-25T14:32:23.12345",
"2019-01-25T14:32:23Z",
"2019-01-25T14:32:23Z-0500"
);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"yyyy-MM-dd['T'[HH:mm:ss][.SSSSS]][z][x]"
); // all the possible combinations
dates.forEach( date -> {
TemporalAccessor accessor = formatter.parseBest(date,
OffsetDateTime::from, // going from most specific date
LocalDateTime::from,
LocalDate::from); // to the less specific
System.out.println( accessor.getClass() + " " + accessor);
}
);
// output for this is
class java.time.LocalDate 2019-01-25
class java.time.LocalDateTime 2019-01-25T14:32:23
class java.time.LocalDateTime 2019-01-25T14:32:23.123450
class java.time.OffsetDateTime 2019-01-25T14:32:23Z
class java.time.OffsetDateTime 2019-01-25T14:32:23-05:00
As to the 2nd question: Yes there is a "timespan" class in the Java JDK.
For a span-of-time not attached to the timeline:
Period
Represents a number of days/weeks/months/years. Can be used in date calculations, automatically accounting for Daylight Savings Time (DST).
For example, to subtract 3 days from a given date, you could do
ZonedDateTime threeDaysAgo = Period.ofDays(-3).addTo(ZonedDateTime.now());
Duration
Similar to Period
but on the scale of days (as 24-hour chunks, not calendar days), hours, minutes, seconds, and fractional second.
ChronoUnit
If you need to do calculations on a wider scale (like include hours/minutes/secods etc) There is also the ChronoUnit
enum:
ZonedDateTime threeHoursAgo = ChronoUnit.HOURS.addTo(ZonedDateTime.now(), -3);