Tuesday, February 22, 2011

Parsing XML time value with java SimpleDateFormat

I have encountered a problem with parsing following string as a date object

2011-02-21T07:00:00.000+01:00

if the only interesting part is getting date and time following date time pattern is enough
yyyy-MM-dd'T'hh:mm:ss

assuming there is following xml node
<startTime>2011-02-23T08:00:00.000+01:00startTime>

//initialization with correct date time pattern
SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");

//timeParam is type of XMLElement

if (timeParam.getName().equals("startTime")) {
Date dateTime = dateTimeFormat .parse(timeParam.getContent());

//dateTime now can be used to set time for GregorianCalendar object
time.startTime = new GregorianCalendar();
time.startTime.setTime(dateTime );

No comments: