Java XMLGregorianCalendar 日期比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1333686/
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-08-12 08:22:28  来源:igfitidea点击:

XMLGregorianCalendar date comparison

javaxml

提问by Ajay

How do i compare 2 instances of XMLGregorianCalendar to find which one is greater? One of the date variables have a value

我如何比较 XMLGregorianCalendar 的 2 个实例以找出哪个更大?日期变量之一具有值

date1 = 2009-02-23T05:54:17+05:30

and the other,

和另一个,

date2 = 2009-02-23T05:54:17.000

采纳答案by skaffman

You could convert them both to GregorianCalendarand compare those (Calendaris Comparable). The semantics compareTo() method of Calendar is explicitly defined, and should work independent of the timezone:

您可以将它们都转换为GregorianCalendar并比较那些 ( Calendaris Comparable)。Calendar 的语义 compareTo() 方法是明确定义的,并且应该独立于时区工作:

Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.

比较由两个 Calendar 对象表示的时间值(距 Epoch 的毫秒偏移量)。

So try this:

所以试试这个:

XMLGregorianCalendar date1 = ...
XMLGregorianCalendar date2 = ...
int result = date1.toGregorianCalendar().compareTo(date2.toGregorianCalendar());

If resultis positive, then date1is "later" than date2

如果result是正数,则date1是“晚于”date2

The compare()method on XMLGregorianCalendaritself does something rather peculiar, and doesn't look very useful to me.

compare()方法XMLGregorianCalendar本身做了一些相当奇特的事情,对我来说看起来不是很有用。