3

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.

BigONotation
  • 4,406
  • 5
  • 43
  • 72
  • 2
    `Z` only covers `-0800` while `X` covers `-08; -0800; -08:00`. you can read it [here](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) – XtremeBaumer Feb 12 '18 at 10:52
  • @XtremeBaumer post it as an answer.... this is what OP needs IMHO – Jordi Castilla Feb 12 '18 at 10:53
  • I was using the following docs https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html. If you look in the example for the zone offsets, you will see that 08:00 is included for the Z pattern. So this is probably an error in the javadoc? – BigONotation Feb 12 '18 at 11:02
  • `Z` seems to have a problem with the `-` before the offset (only if it includes a `:`). `X` is failing at the `:`. But note that the string in your error message does not match the string in your code – XtremeBaumer Feb 12 '18 at 11:07
  • Yes sorry I corrected that – BigONotation Feb 12 '18 at 11:28
  • seems to be a problem witht the `DateTimeFormatter`, because a `SimpleDateFormat` can parse that string without any problems (using `X`) – XtremeBaumer Feb 12 '18 at 11:55
  • @BigONotation see my updated answer – XtremeBaumer Feb 12 '18 at 12:06

2 Answers2

6

From what I found, there seems to definitly be a problem with Z.

I found this question and in the answer, it uses XXX for the offset. I tried it and it is working. I checked a few variations like X,XX,Z,ZZ and ZZZ, but only XXX worked fine.

The complete pattern is dd-MM-yyyy'T'HH:mmXXX

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
0

For this error to be solved, you need to use X instead of Z, so... "dd-MM-yyyy'T'HH:mmX"

If you look at the java docs it explains that:

  • Z allows for -0800
  • X allows for one of the three -08; -0800; -08:00

So for your case being 25-01-2018T15:30-01:00, you need to use the latter.

Community
  • 1
  • 1
Anthony Cannon
  • 1,245
  • 9
  • 20
  • You have sent me a link to SimpleDateFormat. I am using a DateFormatter, and in the docs provided for DateFormatter, Z can take values such as -08:00 – BigONotation Feb 12 '18 at 11:36
  • Maybe you should change to SimpleDateFormat instead? I tested the DateTimeFormatter[1] and out of the following: `V, O, X, x, Z, z`, I could only get `z` to work. Weird, as it shouldn't so maybe this could be a bug on javas side? [1] https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html – Anthony Cannon Feb 12 '18 at 12:08