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
DatatypeFactory object can convert from GregorianCalendar to XMLGregorianCalendar by calling its newXMLGregorianCalendar method. XMLGregorianCalendar xmlGregCal = DatatypeFactory . newInstance() .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With