1

This is the first time I am working this intensly with XML in Java. The code uses JAXB to generate classes and then parse. I have an XML with a date...

A class was generated by JAXB from my XML. It generated the following for the field:

@XmlElement(name = "CoverStartDate", required = true)
protected XMLGregorianCalendar coverStartDate;

In my logic I have the following

xxxx.setCoverStartDate(xmlGregorianCalendar(theDate)

There is a method xmlGregorianCalendar which looks something like this:

GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(date);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);

My return XML that is generated, had the date with a time specified. I only want the date (year-month-day).

Any suggestions?

Thanks

William Brendel
  • 31,712
  • 14
  • 72
  • 77
user990855
  • 67
  • 3
  • 13

4 Answers4

2

Use DatatypeFactory.newXMLGregorianCalendarDate(...) instead of simply using any of the DatatypeFactory.newXMLGregorianCalendar(...) methods.

I don't know what is theDate in your code snippet, however if you're working with Date objects you can use the following.

  public static XMLGregorianCalendar setCoverStartDate(Date date) throws DatatypeConfigurationException {
    Calendar calendar = Calendar.getInstance();
    date.setTime(date.getTime());
    return DatatypeFactory.newInstance().newXMLGregorianCalendarDate(
        calendar.get(Calendar.YEAR),
        calendar.get(Calendar.MONTH) + 1,
        calendar.get(Calendar.DAY_OF_MONTH),
        getTimeZone(calendar));
  }

  public static int getTimeZone(Calendar calendar) {
    return (int) TimeUnit.MINUTES.convert(calendar.get(Calendar.ZONE_OFFSET), TimeUnit.MILLISECONDS);
  }

(Note that calendar's Calendar.ZONE_OFFSET is in milliseconds and the newXMLGregorianCalendarDate(...) method expects the timezone value in minutes, thus it needs to be converted.)

(Also note that Calendar's month index is 0-based, while XMLGregorianCalendar's month is 1-based.)

If this isn't working then the XML schema you've used to generate your JAXB classes is probably erroneous: maybe it does not specify the usage of the xs:date XML schema type (probably it uses xs:dateTime instead).

Only one last advice: create your JAXB classes by hand. Then you can specify annotations like @XmlSchemaType on your classes' fields giving you much more control.

Community
  • 1
  • 1
Kohányi Róbert
  • 9,791
  • 4
  • 52
  • 81
  • +1 for `@XmlSchemaType` for more information on how it can be used with date/time properties see: http://blog.bdoughan.com/2011/01/jaxb-and-datetime-properties.html – bdoughan Oct 12 '11 at 13:10
  • This was great. I implemented it and it is looking a lot better. Now instead of my XML looking like this: 2011-01-01T00:00:00.000+02:00. It looks like this: 2011-01-01+02:00. Is there anything more I can do to get rid of the timezone too...so it only displays the date portion? – user990855 Oct 12 '11 at 14:48
  • Okay...sorry I figured it out. I used DatatypeConstants.FIELD_UNDEFINED as the timezone setting. – user990855 Oct 12 '11 at 14:54
0

This works for me:

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

Date fechaBajaPrevista = Calendar.getInstance().getTime();

XMLGregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(
                        new SimpleDateFormat("yyyy-MM-dd").format(fechaBajaPrevista));
0

You need to use a DateFormatter that formats it into only the DATE part, ignoring the time.

Something like: (new SimpleDateFormat("yyyy MMMM dd")).format(theDate));

Saket
  • 45,521
  • 12
  • 59
  • 79
0

You could leverage a JAXB external binding file to so that a GregorianCalendar is generated into your object model. This will eliminate the need for you to do the conversion. An example of doing something similar can be found here:

The javax.xml.bing.DatatypeConverter class can be useful in creating the necessary XmlAdapter.

bdoughan
  • 147,609
  • 23
  • 300
  • 400