Weird “Unparseable Date” Exception When Using Java’s SimpleDateFormat Class
I found this weird error at my production server, an error which somehow never show up at my testing environment.
java.text.ParseException: Unparseable date: "Thu Oct 11 00:00:00 ICT 2012" at java.text.DateFormat.parse(DateFormat.java:337) at DateClass.main(DateClass.java:7)
It seems that my application is unable to parse the string i provided. But, what makes me confuse is, i try with the same class on my laptop over and over again but this error is never show up. I tought it has something to do with the server’s JDK version or even the server’s operating system.
This is my java class that i use for testing purpose, it runs very well on both my laptop and development server, but it shows error on my server.
import java.text.SimpleDateFormat; import java.util.Date; public class DateClass { public static void main(String[] args) throws Exception { String date = "Thu Oct 11 00:00:00 ICT 2012"; Date tanggalku = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(date); System.out.println(tanggalku); } }
After spend several minutes googling, i found out that it happen because of Java’s Date Localization. This is my workaround,
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class DateClass { public static void main(String[] args) throws Exception { String date = "Thu Oct 11 00:00:00 ICT 2012"; Date tanggalku = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(date); System.out.println(tanggalku); } }
Another workaround, if you are using Apache Tomcat, is adding this configuration on your Tomcat’s server parameter.
-Duser.language=en -Duser.region=US
Hope it helps others 😉
2 Comments
Aditya
about 6 years agoThanks for identifying the problem and its solution.
Replyedwin
about 6 years agoHi Aditya, glad it can help :)