Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLGregorianCalendar date comparison

Tags:

java

xml

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

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

and the other,

date2 = 2009-02-23T05:54:17.000
like image 977
Ajay Avatar asked Aug 26 '09 10:08

Ajay


People also ask

How do you convert GregorianCalendar to XMLGregorianCalendar?

DatatypeFactory object can convert from GregorianCalendar to XMLGregorianCalendar by calling its newXMLGregorianCalendar method. XMLGregorianCalendar xmlGregCal = DatatypeFactory . newInstance() .

How do I find the instance of XMLGregorianCalendar?

XMLGregorianCalendar xCal = ..; //Create instance Date date = xCal. toGregorianCalendar(). getTime(); DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a z"); String formattedString = df. format(date); System.


1 Answers

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

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

So try this:

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

If result is positive, then date1 is "later" than date2

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

like image 184
skaffman Avatar answered Oct 02 '22 13:10

skaffman