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
A one line example using Joda-Time library:
Credit to Nicolas Mommaerts from his comment in the accepted answer.
Check out this code :-
You can see complete example here
I should like to take a step back and a modern look at this 10 years old question. The classes mentioned,
Date
andXMLGregorianCalendar
, are old now. I challenge the use of them and offer alternatives.Date
was always poorly designed and is more than 20 years old. This is simple: don’t use it.XMLGregorianCalendar
is old too and has an old-fashioned design. As I understand it, it was used for producing dates and times in XML format for XML documents. Like2009-05-07T19:05:45.678+02:00
or2009-05-07T17:05:45.678Z
. These formats agree well enough with ISO 8601 that the classes of java.time, the modern Java date and time API, can produce them, which we prefer.No conversion necessary
For many (most?) purposes the modern replacement for a
Date
will be anInstant
. AnInstant
is a point in time (just as aDate
is).An example output from this snippet:
It’s the same as the latter of my example
XMLGregorianCalendar
strings above. As most of you know, it comes fromInstant.toString
being implicitly called bySystem.out.println
. With java.time, in many cases we don’t need the conversions that in the old days we made betweenDate
,Calendar
,XMLGregorianCalendar
and other classes (in some cases we do need conversions, though, I am showing you a couple in the next section).Controlling the offset
Neither a
Date
nor inInstant
has got a time zone nor a UTC offset. The previously accepted and still highest voted answer by Ben Noland uses the JVMs current default time zone for selecting the offset of theXMLGregorianCalendar
. To include an offset in a modern object we use anOffsetDateTime
. For example:Again this conforms with XML format. If you want to use the current JVM time zone setting again, set
zone
toZoneId.systemDefault()
.What if I absolutely need an XMLGregorianCalendar?
There are more ways to convert
Instant
toXMLGregorianCalendar
. I will present a couple, each with its pros and cons. First, just as anXMLGregorianCalendar
produces a string like2009-05-07T17:05:45.678Z
, it can also be built from such a string:Pro: it’s short and I don’t think it gives any surprises. Con: To me it feels like a waste formatting the instant into a string and parsing it back.
Pro: It’s the official conversion. Controlling the offset comes naturally. Con: It goes through more steps and is therefore longer.
What if we got a Date?
If you got an old-fashioned
Date
object from a legacy API that you cannot afford to change just now, convert it toInstant
:Output is the same as before:
If you want to control the offset, convert further to an
OffsetDateTime
in the same way as above.If you’ve got an old-fashioned
Date
and absolutely need an old-fashionedXMLGregorianCalendar
, just use the answer by Ben Noland.Links