Date in XMLGregorianCalendar with a specific format

I get a Date object that I need to convert to XMLGregorian Calendar format

I tried below ways

String formattedDate = sdf.format(categoryData.getBulkCollectionTime()); //yyyy-MM-dd HH:mm:ss XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(formattedDate); dataListType.setTimestamp(xmlCal); 

I get an exception, of course I'm doing wrong here. But I want to format the Date object in the specified format, which is perfectly done by sdf.format.

But how do I create an XMLGregorianCalendar object for the same (from formattedDate)?

+7
source share
2 answers

You can do this by the date object itself:

 String formattedDate = sdf.format(categoryData.getBulkCollectionTime()); //yyyy-MM-dd HH:mm:ss convertStringToXmlGregorian(formattedDate); public XMLGregorianCalendar convertStringToXmlGregorian(String dateString) { try { Date date = sdf.parse(dateString); GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance(); gc.setTime(date); return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc); } catch (ParseException e) { // Optimize exception handling System.out.print(e.getMessage()); return null; } } 
+4
source

You must fix your date format:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); String date = sdf.format(new Date()); XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(date); 
+6
source

All Articles