Sunday, 15 June 2014

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



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

i'm developing webservice integrated spring-struts web application, in xsd there xmlgregoriancalendar type property, let's property name trxdate.

in soapui testing application, if inserted value of trxdate with: 2013-02-21, sent soap xml request info , printed value in service method with: system.out.println(trxdate) method, printout result same inputted: 2013-02-21.

now, i'm trying create function compare trxdate current date. know can compare using trxdate.compare(currentdate) method. problem don't how create xmlgregoriancalendar object set current date date portion (day, month, , year) used comparing.

i tried 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

but i'm expecting: 2013-02-20

if utilize date (xgc) compare trxdate:

int result = trxdate.compare(xgc);

the result 2, means: indeterminate (from datatypeconstants class). proper result should -1, 0, or 1.

so what's wrong code?

instead of trying clear out unwanted fields gregoriancalendar, may easier create un-initialized xmlgregoriancalendar , re-create fields do want:

xmlgregoriancalendar xgc = datatypefactory.newinstance().newxmlgregoriancalendar(); gregoriancalendar = 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 round trip string , 1 time again necessary if utilize newxmlgregoriancalendar(lexicalrepresentation)

java web-services xsd jaxb gregorian-calendar

No comments:

Post a Comment