I am trying to create a ZonedDateTime with a DateTimeFormatter by using the following pattern "dd-mm-yyyy'T'HH:mmZ"
:
public static ZonedDateTime timeFromDayMonthYearHHmmTZ(String dateTime){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy'T'HH:mmZ");
return ZonedDateTime.parse(dateTime, formatter);
}
Using the previous code, the following expression parses correctly:
ZonedDateTime dateTime1 = ZonedDateTimeUtils.dateTimeFromDayMonthYearHHmmTZ("25-01-2018T15:30-0100");
However, the next expression generates an exception (notice the :
in the TZ offset):
ZonedDateTime dateTime2 = ZonedDateTimeUtils.dateTimeFromDayMonthYearHHmmTZ("25-01-2018T15:30-01:00");
My understanding is that Z
in the pattern "dd-mm-yyyy'T'HH:mmZ"
should cover both cases? However I am getting the following exception:
java.time.format.DateTimeParseException: Text '25-01-2018T15:30-01:00' could not be parsed at index 16
Ideally I would like to have a flexible solution where I can parse both patterns.