Monday, July 4, 2011

Transform a date in format yyyy-mm-ddThh:mm:ss

During some testing of 'Ext Calendar Pro' application, I received a date in format 'yyyy-mm-ddThh:mm:ss' from a JSon request. I use SimpleDateFormat class to modify the date with the following pattern :

private static final String CALENDAR_DATE_PATTERN =
"yyyy-MM-dd'T'hh:mm:ss";

This is an example of how I used this pattern to convert my date to a Calendar Java object:

Calendar calendar = Calendar.getInstance(); 
SimpleDateFormat
df =
new SimpleDateFormat(CALENDAR_DATE_PATTERN);
try
{
calendar.setTime(df.parse(value));
}
catch(ParseException pe){
throw new IllegalStateException(pe);
}

No comments: