5

I have short question:

After xjc class generation from xsd, my object require Calendar and thats what I supply. But after marshalling it to XML my 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 binding file but is it possible without creating classes which can parse date and string?

Thanks!

xwhyz
  • 1,444
  • 4
  • 21
  • 45
  • The Date formatter `toString()` will output the date using the current time zone. Depending where your `v` is generated, you could override the `toString()` method to get the result you want. – Bob Dalgleish Sep 03 '13 at 13:31

1 Answers1

0

When you are using Calendar you can set the TimeZone you wish to use:

Java Model

Root

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

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 the default time zone (my environment is Canada/Eastern) and on 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);
    }

}

Output

<?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>
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • well, I dont know why but my output looks like : v="2013-09-03T22:00:00+00:00"/> when I set timezone to GMT – xwhyz Sep 03 '13 at 13:43
  • @xwhyz - Which implementation and version of JAXB are you using? – bdoughan Sep 03 '13 at 13:45
  • @xwhyz - Are you just picking up the JAXB included in your JDK or application server? – bdoughan Sep 03 '13 at 13:53
  • Im using it for a long time with my other xsd files and had no problem, but now I have requirement to change date format – xwhyz Sep 03 '13 at 14:04
  • I've seen that the "Z" vs. "+00:00" varies with the jars you pick up. I'm using SimpleDateFormat with XXXX vs. ZZZZ to force the text to not vary from one environment to another. See http://stackoverflow.com/questions/13568543/how-do-you-specify-the-date-format-used-when-jaxb-marshals-xsddatetime for details on how to setup your own formatter. The text changing like that has an impact on XML digital signing. – johnstosh Oct 20 '15 at 17:57