Java parsing XML date - exclude time
This is the first time I am working this intensly with XML in Java. The code开发者_C百科 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
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.
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));
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:
- http://blog.bdoughan.com/2011/08/xml-schema-to-java-generating.html
The javax.xml.bing.DatatypeConverter
class can be useful in creating the necessary XmlAdapter
.
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));
精彩评论