package com.mycompany.app; | |
import javax.xml.bind.ValidationEventHandler; | |
import javax.xml.bind.annotation.adapters.XmlAdapter; | |
import javax.xml.datatype.DatatypeFactory; | |
import javax.xml.datatype.XMLGregorianCalendar; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
public class DateTimeAdapter extends XmlAdapter<String, XMLGregorianCalendar> { | |
/** | |
* Convert a value type to a bound type. | |
* | |
* @param v The value to be converted. Can be null. | |
* @throws Exception if there's an error during the conversion. The caller is responsible for | |
* reporting the error to the user through {@link ValidationEventHandler}. | |
*/ | |
@Override | |
public XMLGregorianCalendar unmarshal(String v) throws Exception { | |
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX").parse(v); | |
GregorianCalendar c = new GregorianCalendar(); | |
c.setTime(date); | |
XMLGregorianCalendar date2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); | |
return date2; | |
} | |
/** | |
* Convert a bound type to a value type. | |
* | |
* @param v The value to be convereted. Can be null. | |
* @throws Exception if there's an error during the conversion. The caller is responsible for | |
* reporting the error to the user through {@link ValidationEventHandler}. | |
*/ | |
@Override | |
public String marshal(XMLGregorianCalendar v) throws Exception { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment