Converting Between LocalDate and XMLGregorianCalendar – 在LocalDate和XMLGregorianCalendar之间进行转换

最后修改: 2019年 3月 22日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll discuss LocalDate and XMLGregorianCalendar and provide examples of converting between the two types.

在这个快速教程中,我们将讨论LocalDateXMLGregorianCalendar,并提供这两种类型之间转换的例子。

2. XMLGregorianCalendar

2. XMLGregorianCalendar

The XML Schema standard defines clear rules for specifying dates in XML format. In order to use this format, the Java class XMLGregorianCalendar, introduced in Java 1.5, is a representation of the W3C XML Schema 1.0 date/time datatypes.

XML Schema标准定义了明确的规则来指定XML格式的日期。为了使用这种格式,Java类XMLGregorianCalendar在Java 1.5中引入,是W3C XML Schema 1.0日期/时间数据类型的代表。

3. LocalDate

3.本地日期

A LocalDate instance represents a date without a timezone in the ISO-8601 calendar system. As a result, LocalDate is, for example, suitable for storing birthdays but not anything related to time. Java introduced LocalDate in version 1.8.

LocalDate实例表示ISO-8601日历系统中没有时区的日期。因此,例如,LocalDate适合存储生日,但不适合存储与时间有关的东西。Java在1.8版本中引入了LocalDate

4. From LocalDate to XMLGregorianCalendar

4.从LocalDateXMLGregorianCalendar

First, we’ll see how to convert from LocalDate to XMLGregorianCalendar. In order to generate a new instance of XMLGregorianCalendar, we use a DataTypeFactory from the javax.xml.datatype package.

首先,我们来看看如何从LocalDate转换到XMLGregorianCalendar。为了生成一个新的XMLGregorianCalendar的实例,我们使用javax.xml.datatype包中的DataTypeFactory

So, let’s create an instance of LocalDate and convert it to XMLGregorianCalendar:

因此,让我们创建一个LocalDate的实例并将其转换为XMLGregorianCalendar

LocalDate localDate = LocalDate.of(2019, 4, 25);

XMLGregorianCalendar xmlGregorianCalendar = 
  DatatypeFactory.newInstance().newXMLGregorianCalendar(localDate.toString());

assertThat(xmlGregorianCalendar.getYear()).isEqualTo(localDate.getYear());
assertThat(xmlGregorianCalendar.getMonth()).isEqualTo(localDate.getMonthValue());
assertThat(xmlGregorianCalendar.getDay()).isEqualTo(localDate.getDayOfMonth());
assertThat(xmlGregorianCalendar.getTimezone()).isEqualTo(DatatypeConstants.FIELD_UNDEFINED);

As previously noted, an XMLGregorianCalendar instance has the possibility of having timezone information. However, LocalDate doesn’t have any information about time.

如前所述,XMLGregorianCalendar实例有可能具有时区信息。然而,LocalDate没有任何关于时间的信息。

Therefore, when we perform the conversion, the timezone value will remain as FIELD_UNDEFINED.

因此,当我们进行转换时,时区值将保持为FIELD_UNDEFINED

5. From XMLGregorianCalendar to LocalDate

5.从XMLGregorianCalendarLocalDate

Likewise, we’ll now see how to perform the conversion the other way around. As it turns out, converting from a XMLGregorianCalendar to LocalDate is much easier.

同样地,我们现在将看到如何以另一种方式进行转换。事实证明,从XMLGregorianCalendarLocalDate的转换要简单得多。

Again, since LocalDate does not have information about time, a LocalDate instance can contain only a subset of the XMLGregorianCalendar information.

同样,由于LocalDate没有时间信息,LocalDate实例只能包含XMLGregorianCalendar信息的一个子集。

Let’s create an instance of XMLGregorianCalendar and perform the conversion:

让我们创建一个XMLGregorianCalendar的实例并执行转换。

XMLGregorianCalendar xmlGregorianCalendar = 
  DatatypeFactory.newInstance().newXMLGregorianCalendar("2019-04-25");

LocalDate localDate = LocalDate.of(
  xmlGregorianCalendar.getYear(), 
  xmlGregorianCalendar.getMonth(), 
  xmlGregorianCalendar.getDay());

assertThat(localDate.getYear()).isEqualTo(xmlGregorianCalendar.getYear());
assertThat(localDate.getMonthValue()).isEqualTo(xmlGregorianCalendar.getMonth());
assertThat(localDate.getDayOfMonth()).isEqualTo(xmlGregorianCalendar.getDay());

6. Conclusion

6.结语

In this quick tutorial, we’ve covered the transformations between LocalDate instances and XMLGregorianCalendar, and vice-versa.

在这个快速教程中,我们已经介绍了LocalDate实例和XMLGregorianCalendar之间的转换,反之亦然。

And, as always, the sample code is available over on GitHub.

而且,像往常一样,样本代码可以在GitHub上找到