I am getting the time object in the form of a string from a rest service . I need to extract the time and then do some time operation.The given time string is "2015-06-16T14:58:48Z". I tried the below code , to convert the string to the time , however , getting incorrect values.
String time = "2015-06-16T14:58:48Z";
SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-DD'T'hh:mm:ss'Z'", Locale.US);
String dateInString = "2015-06-16T14:58:48Z";
Date date = formatter.parse(dateInString);
System.out.println("Original String : " + time);
System.out.println("After converting to time : " + formatter.format(date));
The output that i am getting is as below: Original String : 2015-06-16T14:58:48Z After converting to time : 2015-12-362T02:58:48Z
The converted date somehow is getting wrong value.Please suggest where is the mistake.Thanks.
You format string has a couple of mistakes:
Y
means the week year, not the year, which isy
D
means the day of the year. You should have usedd
, which means the day of the month.h
means a 12-hour notation time of day. Since you have14
you should useH
, which handle a 24-hour notation.To sum it all up: