字符串到 OffsetDateTime 在 java 中解析
String to OffsetDateTime parse in java
我正在尝试将日期字符串解析为 OffsetDateTime
,如下所示。
但是我遇到了异常,
Exception in thread "main" java.time.format.DateTimeParseException:
Text 'Mon Jun 18 00:00:00 IST 2012' could not be parsed at index 0
public class ParseExample {
public static void main(String... args) throws ParseException {
String dateStr = "Mon Jun 18 00:00:00 IST 2012";
System.out.println(OffsetDateTime.parse(dateStr));
}
}
谁能帮我解决这个错误。
谢谢。
ZonedDateTime
6 月 18 日星期一 00:00:00 IST 2012 应该是 ZonedDateTime
, you can parse it with a custom DateTimeFormatter
, then convert it to OffsetDateTime
:
DateTimeFormatter format = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
OffsetDateTime offsetDateTime = ZonedDateTime.parse(dateStr, format).toOffsetDateTime();
我正在尝试将日期字符串解析为 OffsetDateTime
,如下所示。
但是我遇到了异常,
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mon Jun 18 00:00:00 IST 2012' could not be parsed at index 0
public class ParseExample {
public static void main(String... args) throws ParseException {
String dateStr = "Mon Jun 18 00:00:00 IST 2012";
System.out.println(OffsetDateTime.parse(dateStr));
}
}
谁能帮我解决这个错误。
谢谢。
ZonedDateTime
6 月 18 日星期一 00:00:00 IST 2012 应该是 ZonedDateTime
, you can parse it with a custom DateTimeFormatter
, then convert it to OffsetDateTime
:
DateTimeFormatter format = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
OffsetDateTime offsetDateTime = ZonedDateTime.parse(dateStr, format).toOffsetDateTime();