Charlie-Blake
Charlie-Blake

Reputation: 11050

SimpleDateFormat throws exception

I want to show a date string in this format: Jun 27, 2012. I did this:

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyy");
String date = sdf.format(Calendar.getInstance());

But it gives me this:

java.lang.IllegalArgumentException: Cannot format given Object as a Date

What am I doing wrong?

Upvotes: 1

Views: 1039

Answers (2)

evanwong
evanwong

Reputation: 5134

sdf.format()

Takes a Date, not a Calendar object.

Upvotes: 10

Umesh Aawte
Umesh Aawte

Reputation: 4690

You can not use Calendar.getInstance(), since the param for same is date and not calender instance. Just change the param to getTime()

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
String date = sdf.format(Calendar.getInstance().getTime());

Upvotes: 9

Related Questions