How to compare XMLGregorianCalendar with only the Date portion (day, month, year)?

我与影子孤独终老i 提交于 2019-12-05 21:48:54

Instead of trying to clear out the unwanted fields from the GregorianCalendar, it may be easier to create an un-initialized XMLGregorianCalendar and then copy just the fields you do want:

XMLGregorianCalendar xgc = DatatypeFactory.newInstance().newXMLGregorianCalendar();
GregorianCalendar now = new GregorianCalendar();
xgc.setYear(now.get(Calendar.YEAR));
xgc.setMonth(now.get(Calendar.MONTH) + 1);
xgc.setDay(now.get(Calendar.DAY_OF_MONTH));
System.out.println(xgc);

This avoids the round trip to String and back again that would be necessary if you were to use newXMLGregorianCalendar(lexicalRepresentation)

The javadoc for XMLGregorianCalendar.compare explains that it uses the rules from the XML Schema specification for the comparison, to which the javadoc links.

Section B.1. of the comparison algorithm states that both dateTimes must have exactly the same (sub)set of {year, month, day, hour, minute, second} fields defined. If they don't, the result is indeterminate. (The XML Schema spec uses <> in the algorithm description to indicate an indeterminate result.)

So if you have an XMLGregorianCalendar with just year, month and day defined, you must compare it with another XMLGregorianCalendar with just year, month and day defined. Either you must parse it from a string, as Blaise suggested, or you must instantiate an XMLGregorianCalendar and call setYear, setMonth and setDay on it yourself.

UPDATE

You could also create your XMLGregorianCalendar this way:

    XMLGregorianCalendar xgc = df.newXMLGregorianCalendar(
            2012, 
            DatatypeConstants.FEBRUARY, 
            21, 
            DatatypeConstants.FIELD_UNDEFINED, 
            DatatypeConstants.FIELD_UNDEFINED, 
            DatatypeConstants.FIELD_UNDEFINED, 
            DatatypeConstants.FIELD_UNDEFINED, 
            DatatypeConstants.FIELD_UNDEFINED);
    System.out.println(xgc);

You can use the following method:

  • newXMLGregorianCalendar(String lexicalRepresentation)

Demo

import javax.xml.datatype.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        DatatypeFactory df = DatatypeFactory.newInstance();

        XMLGregorianCalendar xgc = df.newXMLGregorianCalendar("2013-02-12");
        System.out.println(xgc);
    }

}

Output

2013-02-12

try

    String str = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    XMLGregorianCalendar xgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(str);
    System.out.println(xgc);

this is a hint (order) to XMLGregorianCalendar to use xsd:date type where time is undefined

In case anyone is still looking for an alternative. This is what suited my requirements.

    ...

    date.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    date.setTime(DatatypeConstants.FIELD_UNDEFINED,
            DatatypeConstants.FIELD_UNDEFINED,
            DatatypeConstants.FIELD_UNDEFINED,
            DatatypeConstants.FIELD_UNDEFINED);
    ...

This remove the timezone and the timestamp.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!