[英]java.text.ParseException: Unparseable date: “1901-01-01 00:00:00”
This piece of code works correctly in Windows, but in Linux throws a java.text.ParseException: 这段代码在Windows中正常工作,但在Linux中抛出java.text.ParseException:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("es", "ES"));
df.setLenient(false);
Date date = df.parse("1901-01-01 00:00:00");
System.out.println(date);
Windows output: Windows输出:
Tue Jan 01 00:00:00 CET 1901
Linux output: Linux输出:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.simontuffs.onejar.Boot.run(Boot.java:340)
at com.simontuffs.onejar.Boot.main(Boot.java:166)
Caused by: java.text.ParseException: Unparseable date: "1901-01-01 00:00:00"
at java.text.DateFormat.parse(DateFormat.java:357)
...
If a remove the df.setLenient(false)
line, the Windows output is the same, and the Linux exception disappears, however the Linux output seems to be incorrect: 如果删除
df.setLenient(false)
行,则Windows输出相同,并且Linux异常消失,但Linux输出似乎不正确:
Tue Jan 01 00:14:44 CET 1901
Does somebody know what is going on? 有人知道发生了什么吗?
Thanks 谢谢
Configuration: 组态:
Windows: Win7 + jdk1.7.0_71 Windows:Win7 + jdk1.7.0_71
Linux: Ubuntu + jdk1.7.0_60 Linux:Ubuntu + jdk1.7.0_60
EDIT: As anolsi said is a Daylight Saving problem. 编辑:正如anolsi所说,这是一个夏令时问题。 With the date "2015-03-29 02:00:01" the parse exception is thrown, in Windows and Linux, because this date doesn't exist in Madrid (the time was changed from 2:00AM to 3:00AM in Madrid that day).
使用日期“2015-03-29 02:00:01”,在Windows和Linux中抛出了解析异常,因为马德里不存在此日期(时间从马德里凌晨2:00更改为凌晨3:00那天)。 So the correct behaviour is the Linux one.
所以正确的行为是Linux。 The Windows JDK should throw the exception.
Windows JDK应抛出异常。
That should be related with the Locale/Timezone definition you are using. 这应该与您正在使用的Locale / Timezone定义相关。
As you can check under http://www.timeanddate.com/time/change/spain/madrid?year=1901 that specific time didn't exists on that Timezone, because the DST (Daylight Saving Time). 您可以在http://www.timeanddate.com/time/change/spain/madrid?year=1901查看该时区上不存在特定时间,因为DST(夏令时)。 This should be causing the inconsistency.
这应该导致不一致。
If you try instead 1901-02-01 00:00:00
, for instance, it should work fine. 例如,如果您尝试使用
1901-02-01 00:00:00
,它应该可以正常工作。
EDIT1: Example that allow changing and controlling the Timezone. EDIT1:允许更改和控制时区的示例。
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Locale;
import java.util.TimeZone;
import java.util.Date;
public class MainClass
{
public static void main(String[] args)
{
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("es", "ES"));
df.setTimeZone(TimeZone.getTimeZone("Europe/Madrid"));
df.setLenient(false);
Date date = df.parse("1901-01-01 00:00:00");
System.out.println(date);
} catch(Exception ex){
ex.printStackTrace();
}
}
}
EDIT2: Please take a look on the good article regarding timezones and offsets: https://stackoverflow.com/tags/timezone/info 编辑2:请看一下有关时区和抵消的好文章: https : //stackoverflow.com/tags/timezone/info
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.