[英]SimpleDateFormat throws parse Exception for +0100
I am trying with two sets of date with date format : 我正在尝试使用日期格式的两组日期:
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
It works fine for the Date : Fri, 26 Aug 2016 13:55:34 +0000 它适用于日期:星期五,2016年8月26日13:55:34 +0000
Not for the Date : Tue, 06 Sep 2016 11:57:14 +0100 不适用于日期:星期二,2016年9月6日11:57:14 +0100
Throws exception for the +0100 date. 引发+0100日期的异常。
Unparseable date: "Tue, 06 Sep 2016 11:57:14 +0100" (at offset 0)
at java.text.DateFormat.parse(DateFormat.java:555)
It fails at offset 0
, which means that the problem is not related to the timezone but to the day in letters . 它在
offset 0
处失败,这意味着该问题与时区无关,而与字母中的日期无关。
You should set the Locale
of your SimpleDateFormat
. 您应该设置
SimpleDateFormat
的Locale
。
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
Date d1 = format.parse("Fri, 26 Aug 2016 13:55:34 +0000");
Date d2 = format.parse("Tue, 06 Sep 2016 11:57:14 +0100");
Works without any problem. 工作没有任何问题。
If you also need to retrieve the timezone, you will also have to add z
to your pattern: 如果您还需要检索时区,还必须在模式中添加
z
:
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
You need 你需要
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Note the z
for the time zone. 请注意时区的
z
。
The parser ignores the zero ( +0000
) case if z
is not supplied, but not a non-zero ( +0100
) case. 如果未提供
z
,解析器将忽略零( +0000
)情况,但不会忽略非零( +0100
)情况。 The lenient
property controls this behaviour (Acknowledge @Marko Topolnik). 在
lenient
属性控制这种行为(确认@Marko Topolnik)。
Since you're using English week names, you ought to use the two-argument constructor to SimpleDateFormat
, passing Locale.ENGLISH
as the second parameter. 由于您使用的是英文周名称,因此您应该将两参数构造函数用于
SimpleDateFormat
,并将Locale.ENGLISH
作为第二个参数传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.