JAXB date format after XML generation

I have a short question:

After generating the xjc class from xsd, my object requires Calendar, and this is what I will put. But after sorting into XML, the date format is as follows:

<InfoDateTime v="2013-09-03T00:00:00+02:00"/> 

whereas I would like to have:

 <InfoDateTime v="2013-09-03T00:00:00Z"/> 

I do not use annotated jaxb, but with a required file, but is this possible without creating classes that can parse date and string?

Thanks!

+4
source share
1 answer

When you use Calendar , you can set the TimeZone you want to use:

Java Model

Root

Below is a simple Java object that has 2 Calendar fields displayed.

 import java.util.Calendar; import javax.xml.bind.annotation.*; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Root { Calendar defaultTimeZone; Calendar setTimeZone; } 

Demo code

Demo

In the demo code below, we will create two instances of Calendar on defaultTimeZone , it will have a default time zone (my environment is Canada / East), and in setTimeZone we will specify GMT.

 import java.util.*; import javax.xml.bind.*; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Root.class); Root root = new Root(); root.defaultTimeZone = Calendar.getInstance(); root.setTimeZone = Calendar.getInstance(); root.setTimeZone.setTimeZone(TimeZone.getTimeZone("GMT")); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(root, System.out); } } 

Exit

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <root> <defaultTimeZone>2013-09-03T09:40:59.443-04:00</defaultTimeZone> <setTimeZone>2013-09-03T13:40:59.443Z</setTimeZone> </root> 
0
source

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


All Articles