Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date to XMLGregorianCalendar

Tags:

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.

like image 307
Geek Avatar asked Feb 29 '12 02:02

Geek


People also ask

How to Convert String Date to XMLGregorianCalendar?

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.

How to Convert XMLGregorianCalendar into Date in Java?

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.

What is XMLGregorianCalendar format?

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.

What is DatatypeFactory?

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 .


1 Answers

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); 
like image 97
Perception Avatar answered Oct 20 '22 03:10

Perception