Home > Enterprise >  How to add 30 days to a XMLGregorianCalendar variable (Java & Authorize.net)
How to add 30 days to a XMLGregorianCalendar variable (Java & Authorize.net)

Time:01-25

            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(new Date());
            XMLGregorianCalendar startDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
            schedule.setStartDate(startDate); //2020-08-30
            
            XMLGregorianCalendar endDate = (XMLGregorianCalendar) startDate.clone();
            endDate.add(XMLGregorianCalendar.DAY_OF_MONTH, 30);

I'm getting Cannot resolve symbol 'DAY_OF_MONTH'.

Why am I using XMLGregorianCalendar? It's because I'm using Authorize.net as payment gateway and their official docs says it https://developer.authorize.net/api/reference/index.html#recurring-billing-create-a-subscription

Tried endDate.add(DatatypeConstants.FIELD_UNDEFINED, 30); too, but getting same error

CodePudding user response:

From what I can see, the add method expects a Duration argument. Try this: endDate.add(DatatypeFactory.newInstance().newDurationDayTime(true, 30, 0, 0, 0));

CodePudding user response:

Maybe try:

endDate.add(DatatypeFactory.newInstance().newDuration("P30DT0H0M0S"));

CodePudding user response:

Avoid the legacy date-time classes. Do your work using java.time classes. Then at the end convert to a legacy object as required by third-party library.

Capture the current moment as seen in the time zone of interest.

ZoneId z = ZoneId.of( "America/New_York" );
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Add 30 days.

ZonedDateTime laterZdt = zdt.plusDays( 30 ) ;

Convert to legacy object.

XMLGregorianCalendar xgc = 
        DatatypeFactory
        .newInstance()
        .newXMLGregorianCalendar( 
            GregorianCalendar
            .from(
                laterZdt
            )
        ) 
;
  •  Tags:  
  • java
  • Related