Skip to content

Fix Time to Calendar conversion in AbstractXMLGregorianCalendarAdapter #600

@laurentschoelens

Description

@laurentschoelens

From highsource/hyperjaxb3#33

@martinlosse wrote :

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);

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions