juretta.com

Java SimpleDateFormat: Don't forget to specify the locale

March 07, 2006
Tags: Dropbox Java

Assuming your current locale is non US the following piece of code will throw a ParseException: Unparseable date: "06-Mar-2006 12:35"

SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
date = format.parse("06-Mar-2006 12:35");

Note to myself: It's important to specify the correct Locale when parsing dates.

SimpleDateFormat format = 
    new SimpleDateFormat("dd-MMM-yyyy HH:mm", Locale.US);
date = format.parse("06-Mar-2006 12:35");

[1]: http://java.sun.com/.../Locale.html


About

This is the defunct blog of Stefan Saasen.