Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Time to Calendar conversion in AbstractXMLGregorianCalendarAdapter #33

Open
martinlosse opened this issue Dec 5, 2016 · 1 comment

Comments

@martinlosse
Copy link

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);
@highsource
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants