Reputation: 1192
I'm receiving a date as a String like this :2015-07-22.06.05.56.344
. I wrote a parsing code like this
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd.hh.MM.ss.ms");
try {
Date date = sdf.parse("2015-07-22.06.05.56.344");
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And I got the output like this:
Fri May 22 06:03:44 IST 2015
Why is it reading it wrongly? Is it an issue with my code or java cannot recognize this date format?
Upvotes: 0
Views: 18579
Reputation: 2253
You need to use MM
for months and mm
for minutes.
Try setting SimpleDateFormat
to this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd.hh.mm.ss.SSS");
Check javadoc for more info: SimpleDateFormat
Upvotes: 4
Reputation: 347334
MM
/mm
are around the wrong way, mm
is for "Minute in hour" and MM
is for "Month in year"SS
is for "Millisecond" (or ms
, which means nothing)HH
instead of hh
as HH
is for "Hour in day (0-23)"So, using...
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd.HH.mm.ss.SS");
It outputs Wed Jul 22 06:05:56 EST 2015
for me
Upvotes: 4
Reputation: 69494
You have the wrong pattern. The pattern is case sensitive.
mm stands for minutes MM stand for month.
SS is miliseconds
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd.hh.mm.ss.SSS");
For more details see the SimpleDateFormat documentation
Upvotes: 3