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

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for @XmlSchemaType for more information on how it can be used with date/time properties see: blog.bdoughan.com/2011/01/jaxb-and-datetime-properties.html
This was great. I implemented it and it is looking a lot better. Now instead of my XML looking like this: <CoverStartDate>2011-01-01T00:00:00.000+02:00</CoverStartDate>. It looks like this: <CoverStartDate>2011-01-01+02:00</CoverStartDate>. Is there anything more I can do to get rid of the timezone too...so it only displays the date portion?
Okay...sorry I figured it out. I used DatatypeConstants.FIELD_UNDEFINED as the timezone setting.
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));

Comments

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.

Comments

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.