Isn't there a convenient way of getting from a java.util.Date to a XMLGregorianCalendar?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- Illegal to have multiple roots (start tag in epilo
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
I hope my encoding here is right ;D To make it faster just use the ugly getInstance() call of GregorianCalendar instead of constructor call:
Here is a method for converting from a GregorianCalendar to XMLGregorianCalendar; I'll leave the part of converting from a java.util.Date to GregorianCalendar as an exercise for you:
EDIT: Slooow :-)
Assuming you are decoding or encoding xml and using
JAXB
, then it's possible to replace the dateTime binding entirely and use something else than `XMLGregorianCalendar' for every date in the schema.In that way you can have
JAXB
do the repetitive stuff while you can spend the time on writing awesome code that delivers value.Example for a jodatime
DateTime
: (Doing this with java.util.Date would also work - but with certain limitations. I prefer jodatime and it's copied from my code so I know it works...)And the converter:
See here: how replace XmlGregorianCalendar by Date?
If you are happy to just map to an instant based on the timezone+timestamp, and the original timezone is not really relevant, then
java.util.Date
is probably fine too.For those that might end up here looking for the opposite conversion (from
XMLGregorianCalendar
toDate
):Just thought I'd add my solution below, since the answers above did not meet my exact needs. My Xml schema required seperate Date and Time elements, not a singe DateTime field. The standard XMLGregorianCalendar constructor used above will generate a DateTime field
Note there a couple of gothca's, such as having to add one to the month (since java counts months from 0).