java 如何仅将 XMLGregorianCalendar 与日期部分(日、月、年)进行比较?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14978328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:04:10  来源:igfitidea点击:

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

javaweb-servicesxsdjaxbgregorian-calendar

提问by null

I'm developing a webservice integrated with spring-struts web application, in XSD there is a XMLGregorianCalendar type property, let's say the property name is trxDate.

我正在开发一个与 spring-struts 网络应用程序集成的网络服务,在 XSD 中有一个 XMLGregorianCalendar 类型的属性,假设属性名称是trxDate

In SOAPUI testing application, if I inserted the value of trxDate with: 2013-02-21, then I sent the soap xml request data and I printed the value in service method with: System.out.println(trxDate) method, the printout result is same as inputted: 2013-02-21.

在 SOAPUI 测试应用程序中,如果我插入了 trxDate 的值:2013-02-21,那么我发送了soap xml 请求数据并在服务方法中打印了值: System.out.println(trxDate) 方法,打印输出结果与输入相同:2013-02-21。

Now, I'm trying to create a function to compare trxDatewith current date. I know we can compare it using trxDate.compare(currentDate) method. The problem is I don't how to create XMLGregorianCalendar object set with current date with Date portion only (day, month, and year) to be used for comparing.

现在,我正在尝试创建一个函数来将trxDate与当前日期进行比较。我知道我们可以使用 trxDate.compare(currentDate) 方法进行比较。问题是我不知道如何创建带有当前日期的 XMLGregorianCalendar 对象集,仅用于比较的日期部分(日、月和年)。

I tried with this code:

我尝试使用此代码:

    GregorianCalendar gc = new GregorianCalendar();
    gc.set(GregorianCalendar.HOUR_OF_DAY, 0);
    gc.set(GregorianCalendar.MINUTE, 0);
    gc.set(GregorianCalendar.SECOND, 0);
    gc.set(GregorianCalendar.MILLISECOND, 0);

    XMLGregorianCalendar xgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
    System.out.println(xgc);

The result is:
2013-02-20T00:00:00.000+07:00

结果是:
2013-02-20T00:00:00.000+07:00

But I'm expecting:
2013-02-20

但我期待:
2013-02-20

If use the date (xgc) to compare with trxDate:

如果使用日期 ( xgc) 与trxDate进行比较:

int result = trxDate.compare(xgc);

The result is 2, which means: INDETERMINATE (from DatatypeConstants class). The proper result should be -1, 0, or 1.

结果是 2,这意味着:INDETERMINATE(来自 DatatypeConstants 类)。正确的结果应该是 -1、0 或 1。

So what's wrong with my code?

那么我的代码有什么问题?

回答by VGR

The javadoc for XMLGregorianCalendar.compareexplains that it uses the rules from the XML Schema specificationfor the comparison, to which the javadoc links.

XMLGregorianCalendar.comparejavadoc解释说它使用XML Schema 规范中规则进行比较,javadoc 链接到这些规则

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

B.1 节。的比较算法指出,两个日期时间必须具有完全相同的(子)集{年、月、日、小时、分钟、秒}定义的字段。如果他们不这样做,结果是不确定的。(XML Schema 规范<>在算法描述中使用来指示不确定的结果。)

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, setMonthand setDayon it yourself.

因此,如果您有一个只定义了年、月和日的 XMLGregorianCalendar,您必须将它与另一个只定义了年、月和日的 XMLGregorianCalendar 进行比较。要么您必须按照 Blaise 的建议从字符串中解析它,要么您必须实例化一个 XMLGregorianCalendar 并调用setYear,setMonthsetDay自己调用它。

回答by Ian Roberts

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

而不是试图清除从不需要的领域GregorianCalendar,它可能是更容易地创建一个未初始化的XMLGregorianCalendar你,然后只复制领域想要的:

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)

这避免了往返 String 并再次返回,如果您要使用,这将是必要的 newXMLGregorianCalendar(lexicalRepresentation)

回答by bdoughan

UPDATE

更新

You could also create your XMLGregorianCalendarthis way:

你也可以这样创建你的XMLGregorianCalendar

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

回答by Evgeniy Dorofeev

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:datetype where time is undefined

这是对 XMLGregorianCalendar 使用xsd:date时间未定义类型的提示(顺序)

回答by vermac

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.

这将删除时区和时间戳。