Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xs:String in xs:DateTime format to XMLGregorianCalendar

Tags:

java

xml

jaxb

xsd

I use JAXB for un/marshaling XML messages which I get from server. Usually I get the XMLGregorianCalendar values in the fields, that are defind as xs:dateTime in the describing XSD files, so the conversion to XMLGregorianCalendar is done automatically by JAXB.

Example from XSD file

<xs:attribute name="readouttime" use="required" type="xs:dateTime" />

However one field is defined as xs:string like this:

<xs:element minOccurs="1" maxOccurs="1" name="Value" type="xs:string" />

but I am receiving a value that should represent the dateTime:

<Value>2014-08-31T15:00:00Z</Value>

Is there any nice way, how to convert this string to XMLGregorianCallendar, or should I use SimpleDateFormat and type the pattern manually? I feel this may be a dangerous part.

like image 439
Michal Krasny Avatar asked Feb 12 '23 21:02

Michal Krasny


1 Answers

A quick Google search yields

String mydatetime = "2011-09-29T08:55:00";
XMLGregorianCalendar xgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(mydatetime);

Credits go to this blog post.

like image 196
hennes Avatar answered Feb 23 '23 20:02

hennes