我有一个字符串"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)
答案 0 :(得分:4)
单个Z
在时区中不允许:
。使用ZZZZZ
(五个Z)作为扩展格式。
答案 1 :(得分:0)
date
字符串的格式不正确
像这样改变:
String date = "2009-07-16T19:20:30-0500";
请注意timezone
之前的更改。
答案 2 :(得分:0)
如果您想保留日期格式,可以使用以下格式:
String pattern = "yyyy-MM-dd'T'HH:mm:ssXXX";
答案 3 :(得分:0)
试试这个:
String date = "2009-07-16T19:20:30-05:00";
String pattern = "yyyy-MM-dd'T'HH:mm:ssz";
或
<iframe src="https://calendar.google.com/calendar/embed?src=98uemon8cqggilli0hkpbd8vno%40group.calendar.google.com&ctz=Asia/Calcutta" style="border: 0" width="800" height="600" frameborder="0" scrolling="no"></iframe>