import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String dateTimeStr = "Sat, 13 Jun 2020 23:14:21 GMT";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss z");
ZonedDateTime zdt = ZonedDateTime.parse(dateTimeStr, formatter);
System.out.println(zdt);
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);
}
}
输出:
2020-06-13T23:14:21Z[GMT]
2020-06-13T23:14:21
[更新] 由 Basil Bourque 提供
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String dateTimeStr = "Sat, 13 Jun 2020 23:14:21 GMT";
DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
ZonedDateTime zdt = ZonedDateTime.parse(dateTimeStr, formatter);
System.out.println(zdt);
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);
}
}
输出:
2020-06-13T23:14:21Z
2020-06-13T23:14:21