Convert a string to ZonedDateTime

空扰寡人 提交于 2020-05-17 07:44:15

问题


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.


回答1:


    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

  • Wikipedia article: ISO 8601
  • Related question: Cannot parse String in ISO 8601 format, lacking colon in offset, to Java 8 Date



回答2:


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


来源:https://stackoverflow.com/questions/61269697/convert-a-string-to-zoneddatetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!