What's the best way to convert between LocalDate
from Java 8 and XMLGregorianCalendar
?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
The LocalDate stores only year/month/day information. There is no time nor time-zone information in it. The XMLGregorianCalendar stores date (year/month/day) + optionally time and optionally time zone information.
So converting from LocalDate to XMLGregorianCalendar is simple:
Converting from XMLGregorianCalendar to LocalDate might be not so simple, because XMLGregorianCalendar may have time and time-zone information which you simply can't store in LocalDate.
However, I guess that if you are converting from XMLGregorianCalendar to LocalDate then the XMLGregorianCalendar is resulting from a nontimezoned xsd:date element (represented as YYYY-MM-DD in the xml). In that case you should convert it like this:
Whole example:
Converting from
LocalDate
toXMLGregorianCalendar
:Converting back is simpler:
The following is a simple way to convert from LocalDate to XMLGregorianCalendar which both preserves the undefined fields (hours, timezone, etc.) and is efficient (i.e. no conversion to/from String). Unlike some of the other solutions this results in XML dates without timezones, e.g.
<date>2018-11-06</date>
instead of<date>2018-11-06+01:00</date>
.Converting back is a bit simpler:
To convert from
LocalDate
toXMLGregorianCalendar
you can useAnd to convert
XMLGregorianCalendar
back toLocalDate
: