1

I'm trying to parse a string to get a Date object, but it's always returning Sun. December 30, 2012 for the date. Does anyone have any ideas on what I'm doing wrong?

I was using the same code using strings in YYYY-MM-dd format and it worked just fine, so I'm not sure why switching to this format is causing issues.

 public static Date getDateObjFromStr(String dateStr)
{
    DateFormat formatter = new SimpleDateFormat("MM/dd/YYYY");
    Date dateObj;
    try {
        dateObj = formatter.parse(dateStr);
        return dateObj;
    } catch(Exception e) {
        return null;
    }
}

The string representing the date

The Date object returned by the SimpleDateFormat object

rawkfist0215
  • 1,445
  • 6
  • 21
  • 34
  • Instead of returning null, can you print the stack trace when the Exception occurs. – Reji Nov 07 '13 at 23:57

1 Answers1

9

Case-sensitive

Uppercase Y represents week-based year.

Try using lowercase y instead

DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Reimeus
  • 158,255
  • 15
  • 216
  • 276