1

I need to convert the string 2020-04-15T23:00:00+0000 to a ZonedDateTime. I tried the below but they didnt work:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss Z");
System.out.println(ZonedDateTime.parse("2020-04-15T23:00:00+0000", formatter));

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss 00:00");
System.out.println(ZonedDateTime.parse("2020-04-15T23:00:00+0000", formatter));

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss 0000");
System.out.println(ZonedDateTime.parse("2020-04-15T23:00:00+0000", formatter));

I used the ISO_LOCAL_DATE_TIME as well but that didnt work. Any ideas?

Update:

I tried below :

    OffsetDateTime dateTime1 = OffsetDateTime.parse("2020-04-15T23:00:00+0000",
            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"));
    System.out.println(dateTime1.toZonedDateTime());

I get output as 2020-04-15T23:00Z but I need it as 2020-04-15T23:00:00Z with the seconds at the end which this is trimming.

T Anna
  • 874
  • 5
  • 21
  • 52
  • 1
    Does this answer your question? [Java8- ZonedDateTime with DateTimeFormatter not recognizing the format](https://stackoverflow.com/questions/35004123/java8-zoneddatetime-with-datetimeformatter-not-recognizing-the-format) – Dwhitz Apr 17 '20 at 10:51
  • @Dwhitz No it doesnt. – T Anna Apr 17 '20 at 10:55
  • 3
    Your pattern has a space between seconds and the time zone, while the actual values doesn't have a space there. And if you want to print a `ZonedDateTime` in a specific pattern, you should use a formatter with that pattern to format it (eg `formatter.format(dateTime1)`. A `ZonedDateTime` itself has no pattern (it uses a default format when you call `toString()`) – Mark Rotteveel Apr 17 '20 at 11:35
  • @MarkRotteveel Can you please look at my update in the question. I removed the space and it does work but it strips off the seconds from the end. – T Anna Apr 17 '20 at 11:40
  • 2
    The second part of my comment addresses that. When you use `System.out.println(..)` it uses `toString()`, which always uses the same formatter to generate a string representation of the date, and in that formatter, seconds are optional. If you want to use a different format (eg one where seconds aren't optional), you need to explicitly use the formatter (eg `System.out.println(formatter.format(dateTime1))`. – Mark Rotteveel Apr 17 '20 at 11:45

2 Answers2

6
    DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
            .appendPattern("XX")
            .toFormatter();
    String str = "2020-04-15T23:00:00+0000";
    OffsetDateTime dateTime = OffsetDateTime.parse(str, formatter);
    System.out.println(dateTime);

Output:

2020-04-15T23:00Z

Since your input string contains an offset, +0000, and no time zone, prefer to represent the date and time as an OffsetDateTime, not a ZonedDateTime (if you’ve got specific reasons for needing a ZonedDateTime, just use that class instead in the above code, it will work too).

I prefer to use the built-in formatters over writing my own format pattern string. Only for the offset I am using the format pattern XX (there are other patterns that will work too).

I get output as 2020-04-15T23:00Z but I need it as 2020-04-15T23:00:00Z with the seconds at the end which this is trimming.

You most probably don’t. The format is ISO 8601, and in ISO 8601 the seconds are optional when they are 0. The OffsetDateTime or ZonedDateTime has full precision (of nanoseconds, even). The omission (what you called trimming) only happens in the output produced by the toString method of the object. If you need the ISO 8601 format for some other API or other system, the variant without the seconds should work too.

If you do need a particular format, use a second DateTimeFormatter for formatting into a string in that needed format.

What went wrong in your code?

Mark Rotteveel said it already: ISO 8601 allows no space between the time and the UTC offset. Or to put it the other way around, when in your format pattern string you put a space between ss and Z, parsing will require a space in the parsed string. Since there was no space, parsing failed.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

You can convert like following, it supports all of the following input formats

"2012-10-01T19:30:00+0200"
"2012-10-01T19:30:00+02" 
"2012-10-01T19:30:00+02:00"
"2012-10-01T19:30:00Z" 

Code piece

        DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[XXX][XX][X]");


        OffsetDateTime osdt1 = OffsetDateTime.parse("2020-04-15T23:00:01+0000", dtf1);
        OffsetDateTime osdt2 = OffsetDateTime.parse("2020-04-15T23:00:43+0900", dtf1);
        OffsetDateTime osdt3 = OffsetDateTime.parse("2020-04-15T23:00:00+0000", dtf1);
        OffsetDateTime osdt4 = OffsetDateTime.parse("2020-04-15T23:00:00+0900", dtf1);
        System.out.println("2020-04-15T23:00:01+0000 --> " + osdt1.toString());
        System.out.println("2020-04-15T23:00:43+0900 --> " + osdt2.toString());
        System.out.println("2020-04-15T23:00:00+0000 --> " + osdt3.toString());
        System.out.println("2020-04-15T23:00:00+0900 --> " + osdt4.toString());

        System.out.println("\n");

        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
        System.out.println("2020-04-15T23:00:01+0000 --> " + dtf2.format(osdt1));
        System.out.println("2020-04-15T23:00:43+0900 --> " + dtf2.format(osdt2));
        System.out.println("2020-04-15T23:00:00+0000 --> " + dtf2.format(osdt3));
        System.out.println("2020-04-15T23:00:00+0900 --> " + dtf2.format(osdt4));

output of above piece

2020-04-15T23:00:01+0000 --> 2020-04-15T23:00:01Z
2020-04-15T23:00:43+0900 --> 2020-04-15T23:00:43+09:00
2020-04-15T23:00:00+0000 --> 2020-04-15T23:00Z
2020-04-15T23:00:00+0900 --> 2020-04-15T23:00+09:00


2020-04-15T23:00:01+0000 --> 2020-04-15T23:00:01+0000
2020-04-15T23:00:43+0900 --> 2020-04-15T23:00:43+0900
2020-04-15T23:00:00+0000 --> 2020-04-15T23:00:00+0000
2020-04-15T23:00:00+0900 --> 2020-04-15T23:00:00+0900
axnet
  • 5,146
  • 3
  • 25
  • 45