XMLGregorianCalendar Date Comparison

How to compare 2 instances of XMLGregorianCalendar to find which one is bigger? One of the date variables matters

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

and the other

 date2 = 2009-02-23T05:54:17.000 
+7
java xml
source share
1 answer

You can convert both of them to GregorianCalendar and compare them ( Calendar is Comparable ). The semantics compareTo () method for the Calendar is explicitly defined and should work regardless of the time zone:

Compares the time values ​​(millisecond offset from the Epoch) represented by two calendar objects.

So try the following:

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

If result positive, then date1 "later" than date2

The compare() method on XMLGregorianCalendar itself does something rather peculiar and not very useful for me.

+12
source share

All Articles