java.text.ParseException: Unparseable date: "2014/02/20"

I ma getting exeception while parsing the date.

following is the code:

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-DD");
    Date d = format.parse("2014/02/20");
    System.out.println(d);
Jon Skeet
people
quotationmark

Not only have you got the slashes/dashes wrong, you're also using DD (day of year) instead of dd (day of month). You want:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

As always, read the documentation to find out exactly what pattern symbols mean - and if you find it's behaving oddly, check the pattern against the data very carefully.

I would also recommend:

  • Using Joda Time (pre-Java-8) or java.time (Java 8) if you possibly can; the java.util.Date/Calendar API is horrible
  • Specifying the locale explicitly
  • Specifying the time zone explicitly

Currently you're using the default time zone and locale. While the locale may not matter in this case (unless it's used to pick the calendar system; I can't remember offhand) I think it's clearer to explicitly specify it. The time zone definitely matters; if you only ever want to treat this as a date (no time) it's probably worth specifying UTC - that way it's easy to interoperate with anything else, and you get day boundaries in obvious places in the underlying millisecond representation.

people

See more on this question at Stackoverflow