I have date like mm-dd-yyy
in string format and I am trying to convert the same to Date object in java,
Two ways I tried in
"mm-dd-yyy"
which is returning the wrong
date, it always returns month Jan even though the month in the
string is not "01""MM-dd-yyy"
will return the correct date
as expected.But I want to understand why the first approach returning wrong? Can any body tell me the reason.
Date and time formats are specified by pattern strings. Within the pattern strings, m
is interpreted as minute in hour, and M
is interpreted as Month in year.
Perhaps you should read JavaDoc API spec.
And you should check that the year of parsed date object is what you intended to. Since your string is MM-dd-yyy
, "02-12-014"
(which should be 2014-02-12
is actually interpreted as 0014-02-12
, which can be not your intended result.