mosk mosk - 3 months ago 8
Java Question

Date-String parsing problem (due to months from 0 to 11)

The code

String strDate = "2010-12-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
Date parsedDate = sdf.parse(strDate);
System.out.println(parsedDate);


will, dependend on your locale, produce the following output:

Fri Jan 01 00:12:00 CET 2010


The date is not parsed correctly, since i expect the 1st dec and not the 1st jan.
I know, months are numbered from 0 to 11, so the 12 becomes a 0 for january.

I have several solutions for this problem in mind, but all of them will produce at least 3-4 additional lines of code. So my question is:

What is the nicest way to solve this "problem"?

I can't imagine that it takes more than 2-3 lines to parse a simple date...

//edit: Shame on me for this question. Forgive me. thx folks

Answer

change yyyy-mm-dd to yyyy-MM-dd


M   Month in year   Month   July; Jul; 07  

m   Minute in hour  Number  30

See