Java.text.ParseException: Unmatched date: "1901-01-01 00:00:00"

This piece of code works correctly on Windows, but on Linux throws 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:

 Tue Jan 01 00:00:00 CET 1901 

Linux output:

 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 you delete the df.setLenient(false) , the Windows output will be the same and the Linux exception will disappear, however, the Linux output looks incorrect:

 Tue Jan 01 00:14:44 CET 1901 

Does anyone know what is going on?

thanks

Configuration:
Windows: Win7 + jdk1.7.0_71
Linux: Ubuntu + jdk1.7.0_60

EDIT: As anolsi said, this is a problem with summer savings. With the date "2015-03-29 02:00:01", a parsing exception is selected on Windows and Linux because this date does not exist in Madrid (the time was changed from 2:00 to 3:00 in Madrid that day). Therefore, the correct behavior is Linux. Windows JDK should throw an exception.

+6
source share
1 answer

This should be related to the Locale / Timezone definition you are using.

How can you check http://www.timeanddate.com/time/change/spain/madrid?year=1901 that there is no specific time in this time zone, because DST (daylight saving time), This should cause inconsistency .

If you try instead 1901-02-01 00:00:00 , for example, it should work fine.

EDIT1: An example that allows you to change and control the time zone.

 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 at a good article on time zones and offsets: fooobar.com/tags/timezone / ...

+5
source

All Articles