This piece of code works correctly in Windows, but in Linux throws a 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 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:
Tue Jan 01 00:14:44 CET 1901
Does somebody know what is going on?
Thanks
Configuration:
Windows: Win7 + jdk1.7.0_71
Linux: Ubuntu + jdk1.7.0_60
EDIT: As anolsi said is a Daylight Saving problem. 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). So the correct behaviour is the Linux one. The Windows JDK should throw the exception.
That should be related with the Locale/Timezone definition you are using.
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). This should be causing the inconsistency.
If you try instead 1901-02-01 00:00:00
, for instance, it should work fine.
EDIT1: Example that allow changing and controlling the Timezone.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With