微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 如何使用timezone解析datetime,但没有T或纳秒

我正在尝试以下列格式解析日期时间字符串:

2019-02-22 19:29:43+00:00

我正在按照本指南操作:
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

这个特定的行似乎是我正在尝试解析的时间戳字符串:

Z       zone-offset                 offset-Z          +0000; -0800; -08:00;

这是我创建的,给出了指南:

String input = "2019-02-22 19:29:43+00:00";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssX");
LocalDateTime parsed = LocalDateTime.parse(input,formatter);

但我得到这个错误

java.time.format.DateTimeParseException: Text '2019-02-22 19:29:43+00:00' Could not be parsed,unparsed text found at index 22

解决方法

试试yyyy-MM-dd HH:mm:ssXXX.这应该可以解决您的问题.

Offset X and x:

One letter outputs just the hour,such as ‘+01’,unless the minute is non-zero in which case the minute is also output,such as ‘+0130’.

Two letters outputs the hour and minute,without a colon,such as ‘+0130’.

Three letters outputs the hour and minute,with a colon,such as ‘+01:30’.

Four letters outputs the hour and minute and optional second,such as ‘+013015’.

Five letters outputs the hour and minute and optional second,such as ‘+01:30:15’.

Six or more letters throws IllegalArgumentException.

从表格下面的描述:https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

编辑:

如果你想用该模式格式化日期并想要相同的输出字符串,你应该使用yyyy-MM-dd HH:mm:ssxxx(参见下面的Andreas评论).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐