如何在 Java DateUtils.parseDate 中识别祖鲁语时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/424522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I recognize the Zulu time zone in Java DateUtils.parseDate?
提问by Guillaume
I have dates in the format 2008-12-23T00:00:00Z
. This look a lot like a ISO 8601 formatwith a Zulu (UTC) timezone. I though the following code would parse it (using commons-lang) :
我有格式的日期2008-12-23T00:00:00Z
。这看起来很像带有祖鲁语 (UTC) 时区的ISO 8601 格式。我虽然下面的代码会解析它(使用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.
如果我采用相同的模式 ( yyyy-MM-dd'T'HH:mm:ssZZ
) 但删除时区,它就可以工作。
Do you know how I can recognize the Zulu timezone ? I have access only to Java 1.4 and Jakarta commons-lang. No Joda Timefor me yet ...
你知道我如何识别祖鲁时区吗?我只能访问 Java 1.4 和 Jakarta commons-lang。我还没有乔达时间......
采纳答案by Michael Borgwardt
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'
看起来像 commons-lang 中的一个错误FastDateFormat
。告诉他们这件事,你最终应该得到解决。到那时您可以尝试预处理日期并将“Z”替换为“+00”
回答by Argelbargel
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:
我认为 commons-lang 正在使用 java 的内置 DateFormat 或 SimpleDateFormat ,它会为您的日期抛出 ParseException 。如果所有输入字符串都以 Z 结尾,则可以使用以下命令:
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");