Reputation: 3329
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.
Upvotes: 24
Views: 92502
Reputation: 4829
I did the following code to achieve the same. May be a lengthy code but is working fine.
def gregorianCalendar = new GregorianCalendar()
def xmlGregorianCalendar = newInstance().newXMLGregorianCalendar(gregorianCalendar)
if (//past date is needed) {
def date = xmlGregorianCalendar.toGregorianCalendar().time
def cal = Calendar.getInstance()
cal.setTime(date)
cal.add(Calendar.DATE,-3); //subtract 3 days
date.setTime(cal.getTime().getTime())
gregorianCalendar.setTime(date)
xmlGregorianCalendar = newInstance().newXMLGregorianCalendar(gregorianCalendar)
}
Upvotes: 0
Reputation: 80633
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);
Upvotes: 54