【发布时间】: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 上找到了一些类似的东西,并尝试了那里提出的解决方案,但没有奏效。
【问题讨论】:
-
在您的输出格式中使用
mm:ss,而不是MI:SS。 -
仅供参考,诸如
java.util.Date、java.util.Calendar和java.text.SimpleDateFormat等非常麻烦的日期时间类现在是 legacy,被 Java 8 及更高版本中内置的 java.time 类所取代.见Tutorial by Oracle。