I have written this function :
public static XMLGregorianCalendar getXMLGregorianCalendar(String date) throws OmniException{ XMLGregorianCalendar xmlCalender=null; GregorianCalendar calender = new GregorianCalendar(); calender.setTime(Util.stringToJavaDate(date)); xmlCalender = DatatypeFactory.newInstance().newXMLGregorianCalendar(calender); return xmlCalender; } public static Date stringToJavaDate(String sDate) throws OmniException{ Date date=null; date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(sDate); return date; }
When i am passing date as "2014-01-07"
am getting output date as 2014-01-06T18:30:00:000Z
where i am going wrong ? also what to do if I want to get only 2014-01-06T18:30:00
and 2014-01-06T18:30:00Z
Any help is appreciated
Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.
DatatypeFactory object can convert from GregorianCalendar to XMLGregorianCalendar by calling its newXMLGregorianCalendar method. XMLGregorianCalendar xmlGregCal = DatatypeFactory . newInstance() .
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.
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.
Found the solution as below.... posting it as it could help somebody else too :)
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = format.parse("2014-04-24 11:15:00"); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date); XMLGregorianCalendar xmlGregCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal); System.out.println(xmlGregCal);
Output:
2014-04-24T11:15:00.000+02:00
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