Removing the "Z" Part from the XMLGregorianCalender

When I pretend below

GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); calendar.setTime(startTime); // startTime Date DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar); 

I get Output as 2015-04-15T11:04:30.000Z .

I want it to be like 2015-04-15T11:04:30.000 .

Is there any way to achieve this?

+6
source share
5 answers

Do it as it should

 DatatypeFactory df; try { df = DatatypeFactory.newInstance(); return df.newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")); } catch (DatatypeConfigurationException e) { // throw new SomeRuntimeException(e); } 

Or Extend a new class from XMLGregorianCalendar , override toXMLFormat , and then delegate ALL other methods to the contained instance.

 class CustomXMLGregorianCalendar extends XMLGregorianCalendar { XMLGregorianCalendar calendar; CustomXMLGregorianCalendar(XMLGregorianCalendar calendar){ this.calendar = calendar; } public String toXMLFormat() { String text = calendar.toXMLFormat(); int pos = text.indexOf('Z'); return pos < 0 ? text : text.substring(0,pos); } public void setTimezone(int offset){ calendar.setTimezone( offset ); } // ... } 
+1
source

The accepted answer or my Java seems to be deprecated because I got this error: The method newXMLGregorianCalendar(String) in the type DatatypeFactory is not applicable for the arguments (SimpleDateFormat)

and I didnโ€™t want to spread, so I solved it by deleting the time zone:

  xmlCalendar.setTimezone(DatatypeConstants.FIELD_UNDEFINED); 
+8
source

This is because your Locale timezone, to achieve what you need, converts the date using SimpleDateFormat :

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 

UPDATE when you comment:

I tried formatting it to String, and then parse it back to Date. Then set the time in the Calender object. Does not work. However, I get the output as "2015-04-15T11: 04: 30.000Z"

You should understand that Calendar or Date objects are stored in its own format, another thing is how you print them, so in this case, seeing 2015-04-15T11:04:30.000Z in the Calendar view does not matter, you need to have the correct date 2015-04-15 at 11:04:30 to show it in the right format to make your conclusion more convenient.

You get the result from Calendar.toString () , and the doc doc says:

Returns a string representation of this calendar. This method is intended for use only for debugging purposes, and the format of the returned string may vary depending on the implementation . The returned string may be empty, but may not be null.

So, to save the date and time, your Calendar object is correct. To print it, you must convert it to String .

+2
source

If you want to remove only โ€œZโ€ from the XMLGregorianCalendar object, just call this method.

xmlDate.setTimezone (DatatypeConstants.FIELD_UNDEFINED)

+2
source

I had this situation and it was solved using this line:

 XMLGregorianCalendar dateNew = DatatypeFactory.newInstance().newXMLGregorianCalendar("2017-09-06T21:08:14"); 
0
source

Source: https://habr.com/ru/post/987401/


All Articles