hyperjaxb3 icon indicating copy to clipboard operation
hyperjaxb3 copied to clipboard

Fix Time to Calendar conversion in AbstractXMLGregorianCalendarAdapter

Open martinlosse opened this issue 8 years ago • 1 comments
trafficstars

AbstractXMLGregorianCalendarAdapter delegates to concrete implementations for setting varying combinations of fields of Date on an XMLGregorianCalendar.

Two implementations, namely

  • XMLGregorianCalendarAsTime and
  • XMLGregorianCalendarAsDateTime also set milliseconds on the target calendar.

This is done with this line of code: calendar.setMillisecond((int) (date.getTime() % 1000));

The problem here is, that the value resulting from calling date.getTime() may be negative in cases where date is before the beginning of 1970-01-01 (possibly OS dependent). This will leave the modulo of the negative value also negative and cause an IllegalArgumentException as setMillisecond is called with that value.

A possible fix might look as follows:

int msecs = (int) (date.getTime() % 1000); 
calendar.setMillisecond(msecs >= 0 ? msecs : msecs + 1000);

martinlosse avatar Dec 05 '16 19:12 martinlosse

I'm not actively develop this project anymore but would accept a PR.

highsource avatar Dec 05 '16 19:12 highsource