I have a date in the following format in UI..
Eg: Thu. 03/01
I convert them to XMLGregorianCalendar as below explained.
final DateFormat format = new SimpleDateFormat("E. M/d"); final String dateStr = closeDate; final Date dDate = format.parse(dateStr); GregorianCalendar gregory = new GregorianCalendar(); gregory.setTime(dDate); XMLGregorianCalendar dealCloseDate = DatatypeFactory.newInstance() .newXMLGregorianCalendar(gregory);
My Output is "3/06/70 05:00 AM" instead of "3/06/2011 05:00 AM". What is the chnage required to get the proper year.
The simplest way to format XMLGregorianCalendar is to first convert it to the Date object, and format the Date to String. XMLGregorianCalendar xCal = ..; //Create instance Date date = xCal. toGregorianCalendar(). getTime(); DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a z"); String formattedString = df.
Since java. util. Date is the most common way to deal with Java date and time, we always need to convert the instance of XMLGregorianCalendar to the Java Date instance. Using the Java API, we can easily convert XMLGregorianCalendar to XMLGregorianCalendar Date and Date in Java.
The XML Schema standard defines clear rules for specifying dates in XML format. In order to use this format, the Java class XMLGregorianCalendar, introduced in Java 1.5, is a representation of the W3C XML Schema 1.0 date/time datatypes.
DatatypeFactory ", exists, a class with the name of the property's value is instantiated. Any Exception thrown during the instantiation process is wrapped as a DatatypeConfigurationException . If the file ${JAVA_HOME}/lib/jaxp. properties exists, it is loaded in a Properties Object .
You didn't mention anything about how the year is supposed to be represented in this date conversion, but here is some pseudocode to get you started. Note that I don't explicitly deal with the timezone in this example:
final DateFormat format = new SimpleDateFormat("E. M/d"); final String dateStr = "Thu. 03/01"; final Date date = format.parse(dateStr); GregorianCalendar gregory = new GregorianCalendar(); gregory.setTime(date); XMLGregorianCalendar calendar = DatatypeFactory.newInstance() .newXMLGregorianCalendar( gregory);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With