daydreamer
daydreamer

Reputation: 91949

Java SimpleDateFormat gives parse error

I am doing the following

SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); calendar.setTime(formatter.parse("01/26/2012");

When I do

calendar.getTime()

I see

java.text.ParseException: Unparseable date: "01/26/2012"

What is am I doing wrong?

Thank you

Upvotes: 1

Views: 1787

Answers (3)

Peter Svensson
Peter Svensson

Reputation: 6173

You've set the format to MM-dd-yyyy And the you expect it to parse the string on a format: MM/dd/yyyy

Try

 new SimpleDateFormat("MM/dd/yyyy");

Upvotes: 2

user800014
user800014

Reputation:

SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); 

Upvotes: 2

Brian Roach
Brian Roach

Reputation: 76888

Erm, you give the pattern

MM-dd-yyyy

to SimpleDateFormat then offer it

01/26/2012

to parse. That's not the format you specified, and it's telling you it can't parse it.

You would need:

SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); 

Upvotes: 6

Related Questions