I have a file that can have a date modified value with the format of a date or date time. I used to parse the value as:
String t = "2012-01-05T21:21:52.834Z";
logger.info(ZonedDateTime.parse(t).toEpochSecond() * 1000);
Now, the string could also be
t = "2012-01-05";
which threw an error
Exception in thread "main" java.time.format.DateTimeParseException: Text '2012-01-05' could not be parsed at index 10 at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
If I do this string with (Reference)
LocalDate date = LocalDate.parse(t, DateTimeFormatter.ISO_DATE);
logger.info(date.atStartOfDay(ZoneId.of("UTC")).toEpochSecond() * 1000);
This would work. However, as I have mentioned that string could be either of these types, how can I identify the format and then get the millis accordingly?