[英]Not able to parse String to Java Date
我有一个字符串"2017-01-03T02:20:52+00:00"
我想转换为LocalDateTime
。
我尝试了下面的代码
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
String date = "2009-07-16T19:20:30-05:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime dateTime = LocalDateTime.parse(date, inputFormatter);
System.out.println(dateTime);
}
}
我尝试了各种模式,但是每次遇到以下错误时:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2009-07-16T19:20:30-05:00' could not be parsed at index 19
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at HelloWorld.main(HelloWorld.java:11)
单个Z
不允许在时区中使用:
。 使用ZZZZZ
(五个Z)表示扩展格式。
您date
字符串的格式不正确
像这样更改它:
String date = "2009-07-16T19:20:30-0500";
注意timezone
之前的更改。
如果要保留日期格式,可以使用以下模式:
String pattern = "yyyy-MM-dd'T'HH:mm:ssXXX";
尝试这个:
String date = "2009-07-16T19:20:30-0500";
String pattern = "yyyy-MM-dd'T'HH:mm:ssZ";
要么
String date = "2009-07-16T19:20:30-05:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ssz";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.