How to convert a String to a Date using SimpleDateFormat?

Try this:

new SimpleDateFormat("MM/dd/yyyy")
  • MM is “month” (not mm)
  • dd is “day” (not DD)

It’s all in the javadoc for SimpleDateFormat

FYI, the reason your format is still a valid date format is that:

  • mm is “minutes”
  • DD is “day in year”

Also, you don’t need the cast to Date… it already is a Date (or it explodes):

public static void main(String[] args) throws ParseException {
    System.out.println(new SimpleDateFormat("MM/dd/yyyy").parse("08/16/2011"));
}

Output:

Tue Aug 16 00:00:00 EST 2011

Voila!

Leave a Comment