Pergunta

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

  1. sending the date format as "mm-dd-yyy" which is returning the wrong date, it always returns month Jan even though the month in the string is not "01"
  2. sending the date format as "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.

Foi útil?

Solução 2

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.

Outras dicas

See the Format

M   Month in year 

m   Minute in hour 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top