hyperjaxb3
hyperjaxb3 copied to clipboard
Fix Time to Calendar conversion in AbstractXMLGregorianCalendarAdapter
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);
I'm not actively develop this project anymore but would accept a PR.