My client sends me dates in ISO8601 which sometimes contains millisecond with zone and sometimes without millisecond and zone.
for example: 2019-05-01T06:55:43+01:00, 2019-05-01T06:55
I am using new time Java 8 API(LocalDateTime
, DateTimeFormatter
etc).
I didn't find a Date
class with formatter which can handle both, with millisecond and zone and without this.
I am trying do do input date validation by using a single format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(templateFormat, locale);
try {
ldt = LocalDateTime.parse(context, formatter );
String result = ldt.format(formatter);
return result.equals(context);
} catch (DateTimeParseException e) {
try {
LocalDate ld = LocalDate.parse(context, formatter );
String result = ld.format(formatter);
return result.equals(context);
I am asking if there is a formatter which caches both, i can use multiple date classes, and if i fail continue to the next, for example (LocalDateTime, LocalDate, OffsetDateTime) but i need to use a single formatter