How can I recognize the Zulu time zone in Java DateUtils.parseDate?

主宰稳场 提交于 2019-12-31 21:44:56

问题


I have dates in the format 2008-12-23T00:00:00Z. This look a lot like a ISO 8601 format with a Zulu (UTC) timezone. I though the following code would parse it (using commons-lang) :

String pattern = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern();
Date d = DateUtils.parseDate(dateToParse, new String[] { pattern });

If I take the same pattern (yyyy-MM-dd'T'HH:mm:ssZZ) but remove the timezone, it works.

Do you know how I can recognize the Zulu timezone ? I have access only to Java 1.4 and Jakarta commons-lang. No Joda Time for me yet ...


回答1:


Looks like a bug in commons-lang's FastDateFormat. Tell them about it, and you should get a fix eventually. Till then you could try to preprocess the dates and replace 'Z' with '+00'




回答2:


I think commons-lang is using java's built-in DateFormat or SimpleDateFormat which throws a ParseException for your date. If all your input strings end with the trailing Z, you could use this:


java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
// explicitly set timezone of input if needed
df.setTimeZone(java.util.TimeZone.getTimeZone("Zulu"));
java.util.Date date = df.parse("2008-12-23T00:00:00Z");


来源:https://stackoverflow.com/questions/424522/how-can-i-recognize-the-zulu-time-zone-in-java-dateutils-parsedate

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