将字符串转换为ZonedDateTime [英] Convert a string to ZonedDateTime

查看:1032
本文介绍了将字符串转换为ZonedDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将字符串2020-04-15T23:00:00 + 0000转换为ZonedDateTime.我尝试了以下方法,但它们没有起作用:

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));

我也使用了ISO_LOCAL_DATE_TIME,但这没有用.有什么想法吗?

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

更新:

我在下面尝试过:

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

我得到的输出为2020-04-15T23:00Z,但我需要输出为2020-04-15T23:00:00Z,并在结尾处加上秒数.

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.

推荐答案

    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);

输出:

2020-04-15T23:00Z

2020-04-15T23:00Z

由于您的输入字符串包含一个偏移量+0000,并且没有时区,因此,您最好将日期和时间表示为OffsetDateTime,而不是ZonedDateTime(如果有特定的原因需要使用ZonedDateTime,只需在上面的代码中使用该类,它也将起作用).

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).

我更喜欢使用内置的格式化程序,而不是编写自己的格式模式字符串.仅对于偏移量,我使用的是格式模式XX(其他样式也可以使用).

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).

我得到的输出为2020-04-15T23:00Z但我需要它 2020-04-15T23:00:00Z,以秒为结尾,这是 修剪.

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.

您很可能不会.格式为ISO 8601,在ISO 8601中,秒为0时是可选的.OffsetDateTimeZonedDateTime具有全精度(甚至是纳秒).省略(您称为修整)仅发生在对象的toString方法产生的输出中.如果某些其他API或其他系统需要ISO 8601格式,则不带秒的变体也应该起作用.

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.

如果确实需要特定格式,请使用第二个DateTimeFormatter格式化为所需格式的字符串.

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

马克·罗特韦尔(Mark Rotteveel)已经说过:ISO 8601在时间和UTC偏移量之间不允许有空格.或者反过来说,当在格式模式字符串中的ssZ之间放置一个空格时,解析将需要在解析的字符串中有一个空格.由于没有空间,因此解析失败.

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.

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

这篇关于将字符串转换为ZonedDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆