意外的 java SimpleDateFormat 解析异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/781257/
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
Unexpected java SimpleDateFormat parse exception
提问by Emilio
I can't understand why this few lines
我不明白为什么这几行
Date submissionT;
SimpleDateFormat tempDate = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
public time_print(String time) {
try {
submissionT=tempDate.parse(time);
}
catch (Exception e) {
System.out.println(e.toString() + ", " + time);
}
}
Cause exceptions and print out
引发异常并打印出来
java.text.ParseException: Unparseable date: "Tue Mar 31 06:09:00 CEST 2009", Tue Mar 31 06:09:00 CEST 2009
... while the "unparsable" time is compliant with the format string i've passed to SimpleDateFormat().. Any Idea?
...而“不可解析”时间符合我传递给 SimpleDateFormat() 的格式字符串.. 有什么想法吗?
采纳答案by kgiannakakis
It is a Locale issue. Use:
这是语言环境问题。用:
sdf = SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US);
回答by Yuval Adam
Works for me.
为我工作。
public class Main {
public static void main(String[] args)
{
time_print("Tue Mar 31 06:09:00 CEST 2009");
}
static Date submissionT;
static SimpleDateFormat tempDate = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
public static void time_print(String time) {
try {
submissionT=tempDate.parse(time);
System.out.println(submissionT);
}
catch (Exception e) {
System.out.println(e.toString() + ", " + time);
}
}
}
}
回答by Matt Large
The 'z' in your format represents TimeZone and Java only recognises certain timezone ID's. You can get the list out of the TimeZone class as a String Array. CEST does not appear in the list I just generated from JDK 1.5
格式中的“z”代表时区,Java 仅识别某些时区 ID。您可以从 TimeZone 类中获取列表作为字符串数组。CEST 没有出现在我刚从 JDK 1.5 生成的列表中
String[] aZones = TimeZone.getAvailableIDs();
for (int i = 0; i < aZones.length; i++) {
String string = aZones[i];
System.out.println(string);
}
Hope this helps.
希望这可以帮助。