0

As I am not expert in handling of dates in java but I am unable to understand this behaviour.
Here is my code

Date from = new SimpleDateFormat("dd/MM/yyyy").parse("05/07/2013");
System.out.println(from);


which gave me this output

Sat Jul 05 00:07:00 PKT 2013


And this is 2nd another code snippet

Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");
System.out.println(from);


which gave me this output:
Sat Jan 05 00:07:00 PKT 2013

Now the thing which is considerable is format.
This format dd/MM/yyyy which have MM gave me correct output but this format dd/mm/yyyy which have small mm gave me wrong output (always give jan in month).I read the doc where it is mentioned that samll m is for minutes and capital M is for month
My question is Can I never use small m here? if no , then why it is giving the result and on which basis it is giving jan everytime

I know this is a basic question but after searching and after not finding any understandable thing , I posted it.Thanks

Despicable
  • 3,797
  • 3
  • 24
  • 42

5 Answers5

4
Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");

that mm in your format is for minutes. MM is for month.

Those formatting placeholders are fixed. small m is always for minutes. And it's January because this is the default Month value.

Marco Forberg
  • 2,634
  • 5
  • 22
  • 33
4

mm is for minutes so you do not have any month in your date. Thus, I guess that the month is initialized to 0 (Jan)

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
0

The reason it does not fail is because by default the formatter is lenient. If you want it to fail then setLenient(false) on the formatter object.

Although I do not think it will fail in your case as in your example it will read 07 as minutes.

Bhushan Bhangale
  • 10,921
  • 5
  • 43
  • 71
0

When using

Date from = new SimpleDateFormat("dd/mm/yyyy").parse("05/07/2013");

the simple mm is for minutes therefore i think the value for month is assumed to be 0 so it gives Jan as the default value so use MM for month.

pulasthi
  • 1,730
  • 1
  • 17
  • 29
0

I guess it comes from calendar.clear() which will point to 1 Jan 1970. Then it adds you parsed data = 2013 years (YY), 5 days (dd) and 7 minutes (mm). Use MM for month

Rafael T
  • 15,401
  • 15
  • 83
  • 144