我有一个字符串 "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)作为扩展格式。
关于java - 无法将字符串解析为 Java 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46642081/