Skip to content

Instantly share code, notes, and snippets.

@cosminpopescu14
Created October 16, 2020 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosminpopescu14/a29e7ebbcafd784574f05a8f05fe7557 to your computer and use it in GitHub Desktop.
Save cosminpopescu14/a29e7ebbcafd784574f05a8f05fe7557 to your computer and use it in GitHub Desktop.
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