Java中OffsetDateTime解析的字符串 [英] String to OffsetDateTime parse in java

查看:550
本文介绍了Java中OffsetDateTime解析的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将日期字符串解析为 OffsetDateTime ,如下所示。

I am trying to parse date string to OffsetDateTime as below.

但是我遇到了以下异常,

But I am getting below exception,


线程 main中的异常java.time.format.DateTimeParseException:
Text'Mon Jun 18 00:00:00 IST 2012'无法在索引0处解析

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.

谢谢。

推荐答案

ZonedDateTime



周一6月18日00:00:00 IST 2012 应该是 ZonedDateTime ,您可以使用自定义 DateTimeFormatter ,然后将其转换为 OffsetDateTime

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();

这篇关于Java中OffsetDateTime解析的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆