在SimpleDateFormat中日期解析行为不当 [英] Misbehaving of the date parsing in SimpleDateFormat
问题描述
我在JBoss服务器的jar文件中有一个Java类,该类通过bash文件调用,如下所示.
I have a java class inside the jar file that is in a JBoss server which invoked through a bash file as follows.
java -cp /com/site/domain/TimeFormatter.jar packOne.subPack.Test
但是在其中解析以下日期时出现错误.
But I got an error when parsing the below date in there.
java.text.ParseException: Unparseable date: "Wed, 29 Jan 2014 21:00:00 GMT"
at java.text.DateFormat.parse(DateFormat.java:335)
Java代码:
Date date = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z").parse("Sun, 26 Jan 2014 21:00:00 GMT");
System.out.println("main().date : " + date);
我在这里想念什么?我读过许多有关SimpleDateFormat行为异常的文章.但这是我在那些帖子中找不到的东西.
What am I missing here? I read many posts about the misbehaving of the SimpleDateFormat. But this is some thing that I didn't find among those posts.
注意:我认为这是由于Java版本更改或某些原因引起的.但是,我在计算机中执行了不同版本的同一程序.效果很好. 服务器Java版本"1.5.0_22"
Note : I thought this is because of the java version change or some thing. But, I executed the same program with different versions in my machine. It worked perfectly. Server java version "1.5.0_22"
语言环境详细信息:
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
推荐答案
对我来说很好用,我认为这不是与 Java版本相关的问题...而是更多的Locale
问题:)
Works fine for me, and I think it's not a Java version-related issue... but more of a Locale
problem :)
为您的SimpleDateFormat
指定Locale.ENGLISH
应该可以使其正常工作.例如,您可以这样做:
Specifying Locale.ENGLISH
for your SimpleDateFormat
should definitively make it work. For example, you could do:
final Date date = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.ENGLISH).parse("Sun, 26 Jan 2014 21:00:00 GMT");
按照 SimpleDateFormat的javadoc条目中的指定
,日期解析是 locale-sensitive .我必须承认我不完全知道您的字符串与您的Locale,
不匹配,但是如果...我们知道您的默认Locale
实际上是什么,我们可以进一步调查:)
As specified in the javadoc entry for SimpleDateFormat, the date parsing is locale-sensitive. I have to admit that I don't exactly know how your string does not match your Locale,
but we can investigate further if... we know what your default Locale
actually is :)
这篇关于在SimpleDateFormat中日期解析行为不当的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!