【问题标题】:Not able to parse UTC date time to EST local time [duplicate]无法将 UTC 日期时间解析为 EST 本地时间 [重复]
【发布时间】:2016-09-09 03:14:29
【问题描述】:

将 UTC 日期时间解析为 EST 本地时间时出现以下异常。

例外:

Stacktrace:] with root cause
 java.text.ParseException: Unparseable date: "2016-09-09T03:00:29Z"
    at java.text.DateFormat.parse(DateFormat.java:357)
    at com.study.crud.util.GenericUtils.convertUTCDateToEST(GenericUtils.java:55)

GenericUtils.java

public static String convertUTCDateToEST(String utcDate) throws ParseException {
        SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
        inFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date aDate = inFormat.parse(utcDate);

        DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:MI:SS");
        outFormat.setTimeZone(TimeZone.getTimeZone("EST"));
        String estDate = outFormat.format(aDate);

        return estDate;
    }

java.text.ParseException: Unparseable date "yyyy-MM-dd'T'HH:mm:ss.SSSZ" - SimpleDateFormat 的 SO 上找到了一些类似的东西,并尝试了那里提出的解决方案,但没有奏效。

【问题讨论】:

标签: java date datetime utc


【解决方案1】:

输入格式有误。您已将毫秒指定为格式 .SSS 的输入,然后是 Z 区。

但是,您还没有通过毫秒选项。以下格式有效

SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

【讨论】:

【解决方案2】:

这是因为您的输入中缺少millisecond 部分时间:

"2016-09-09T03:00:29Z" 
                    ^ unmatched place

您可以通过添加缺失的部分来修改输入 (2016-09-09T03:00:29.000Z) 或相应地更改您的格式模式 (yyyy-MM-dd'T'HH:mm:ss'Z')。

另外,你的输出格式有错别字(时间的分钟部分):

"yyyy-MM-dd HH:MI:SS"

应该替换为yyyy-MM-dd HH:mm:ss

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 2021-06-29
    • 2023-03-23
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    相关资源
    最近更新 更多