String to OffsetDateTime parse in java

和自甴很熟 提交于 2019-12-11 17:11:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!