问题
I am trying to parse date string to OffsetDateTime
as below.
But I am getting below exception,
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));
}
}
can someone please help me with this mistake.
Thanks.
回答1:
ZonedDateTime
Mon Jun 18 00:00:00 IST 2012 should be a 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();
来源:https://stackoverflow.com/questions/54734921/string-to-offsetdatetime-parse-in-java