Convert String Date to XMLGregorianCalendar in Java – Java 中将字符串日期转换为 XMLGregorianCalendar

最后修改: 2024年 2月 5日

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

1. Overview

1.概述

In this tutorial, we’ll look at various approaches to converting a String Date to an XMLGregorianCalendar.

在本教程中,我们将介绍将 String Date 转换为 XMLGregorianCalendar 的各种方法。

2. XMLGregorianCalendar

2. XMLGregorianCalendar</em

The XML Schema standard defines clear rules for specifying dates in XML format. To use this format, the Java class XMLGregorianCalendar, introduced in Java 1.5, represents the W3C XML Schema 1.0 date/time datatypes.

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

The DatatypeFactory class from the javax.xml.datatype package provides factory methods for creating instances of various XML schema built-in types. We’ll use this class to generate a new instance of XMLGregorianCalendar.

javax.xml.datatype 包中的 DatatypeFactory 类为创建各种 XML 架构内置类型的实例提供了工厂方法。我们将使用该类生成 XMLGregorianCalendar 的新实例。</em

3. String Date to XMLGregorianCalendar

3.字符串日期到XMLGregorianCalendar

First, we’ll see how to convert a String date without a timestamp to XMLGregorianCalendar. The pattern yyyy-MM-dd is commonly used to represent dates.

首先,我们将了解如何将不带时间戳String 日期转换为 XMLGregorianCalendaryyyy-MM-dd模式通常用于表示日期。

3.1. Using Standard DatatypeFactory

3.1.使用标准 DatatypeFactory

Here’s an example using DatatypeFactory for parsing date strings into XMLGregorianCalendar:

下面是一个使用 DatatypeFactory 将日期字符串解析为 XMLGregorianCalendar 的示例:

XMLGregorianCalendar usingDatatypeFactoryForDate(String dateString) throws DatatypeConfigurationException {
    return DatatypeFactory.newInstance().newXMLGregorianCalendar(dateString);
}

In the above example, the newXMLGregorianCalendar() method creates an XMLGregorianCalendar instance from a String representation of a date. The date that we’ve provided follows the XML schema dateTime data type.

在上面的示例中,newXMLGregorianCalendar() 方法根据日期的字符串表示创建了一个 XMLGregorianCalendar 实例。我们提供的日期遵循 XML 模式 dateTime 数据类型。

Let’s create an instance of XMLGregorianCalendar by performing the conversion:

让我们通过执行转换来创建 XMLGregorianCalendar 的实例:

void givenStringDate_whenUsingDatatypeFactory_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException {
    String dateAsString = "2014-04-24";
    XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingDatatypeFactoryForDate(dateAsString);
    assertEquals(24, xmlGregorianCalendar.getDay());
    assertEquals(4, xmlGregorianCalendar.getMonth());
    assertEquals(2014, xmlGregorianCalendar.getYear());
}

3.2. Using LocalDate

3.2.使用 LocalDate

LocalDate is an immutable, thread-safe class. Moreover, a LocalDate can hold only date values and not have a time component. In this approach, we’ll first convert the string date to LocalDate instance, and then we’ll convert it again into XMLGregorianCalendar:

LocalDate 是一个不可变、线程安全的类此外,LocalDate 可以仅保存日期值,而不包含时间组件。在这种方法中,我们首先将字符串日期转换为 LocalDate 实例,然后再将其转换为 XMLGregorianCalendar 实例:

XMLGregorianCalendar usingLocalDate(String dateAsString) throws DatatypeConfigurationException {
    LocalDate localDate = LocalDate.parse(dateAsString);
    return DatatypeFactory.newInstance().newXMLGregorianCalendar(localDate.toString());
}

Let’s see the following test code:

让我们看看下面的测试代码:

void givenStringDateTime_whenUsingApacheCommonsLang3_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException {
    XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingLocalDate(dateAsString);
    assertEquals(24, xmlGregorianCalendar.getDay());
    assertEquals(4, xmlGregorianCalendar.getMonth());
    assertEquals(2014, xmlGregorianCalendar.getYear());
}

4. String Date and Time to XMLGregorianCalendar

4.字符串日期和时间到 XMLGregorianCalendar

Now, we’ll see multiple approaches to converting a String date with timestamp to XMLGregorianCalendar. The pattern yyyy-MM-dd’T’HH:mm:ss is commonly used to represent dates and times in XML.

现在,我们将看到将带有 timestamp 的字符串日期转换为 XMLGregorianCalendar 的多种方法。yyyy-MM-dd’T’HH:mm:ss模式通常用于在 XML 中表示日期和时间。

4.1. Using SimpleDateFormat Class

4.1.使用 SimpleDateFormat Class

One of the traditional approaches to convеrt a date with timеstamp to an XMLGregorianCalendar is by using the SimplеDatеFormat class. Let’s create an instance of XMLGregorianCalendar from dateTimeAsString:

将带有 timеstamp 的日期转换为 XMLGregorianCalendar 的传统方法之一是使用 SimplеDatеFormat 类。让我们从 dateTimeAsString 中创建一个 XMLGregorianCalendar 实例:

XMLGregorianCalendar usingSimpleDateFormat(String dateTimeAsString) throws DatatypeConfigurationException, ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date date = simpleDateFormat.parse(dateTimeAsString);
    return DatatypeFactory.newInstance().newXMLGregorianCalendar(simpleDateFormat.format(date));
}

We’re using SimpleDateFormat to parse the input String dateTime to a Date object and then format the Date object back into String before creating an XMLGregorianCalendar instance.

我们使用 SimpleDateFormat 将输入字符串 dateTime 解析为 Date 对象,然后在创建 XMLGregorianCalendar 实例之前,将 Date 对象格式化为字符串。

Let’s test this approach:

让我们来测试一下这种方法:

void givenStringDateTime_whenUsingSimpleDateFormat_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException, ParseException {
    XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingSimpleDateFormat(dateTimeAsString);
    assertEquals(24, xmlGregorianCalendar.getDay());
    assertEquals(4, xmlGregorianCalendar.getMonth());
    assertEquals(2014, xmlGregorianCalendar.getYear());
    assertEquals(15, xmlGregorianCalendar.getHour());
    assertEquals(45, xmlGregorianCalendar.getMinute());
    assertEquals(30, xmlGregorianCalendar.getSecond());
}

4.2. Using GregorianCalendar Class

4.2.使用 GregorianCalendar Class

GregorianCalendar is a concrete implementation of the abstract class java.util.Calendar. Let’s use the GregorianCalendar class to convert a String date and time to an XMLGregorianCalendar:

GregorianCalendar 是抽象类 java.util.Calendar 的具体实现。让我们使用 GregorianCalendar 类将字符串日期和时间转换为 XMLGregorianCalendar

XMLGregorianCalendar usingGregorianCalendar(String dateTimeAsString) throws DatatypeConfigurationException, ParseException {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(dateTimeAsString));
    return DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
}

First, we’re creating an instance of GregorianCalendar and setting its time based on the parsed Date. After that, we’re using a DatatypeFactory to create an XMLGregorianCalendar instance. Let’s test this approach:

首先,我们创建一个 GregorianCalendar 实例,并根据解析后的 Date 设置其时间。然后,我们使用 DatatypeFactory 创建 XMLGregorianCalendar 实例让我们测试一下这种方法:

void givenStringDateTime_whenUsingGregorianCalendar_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException, ParseException {
    XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingGregorianCalendar(dateTimeAsString);
    assertEquals(24, xmlGregorianCalendar.getDay());
    assertEquals(4, xmlGregorianCalendar.getMonth());
    assertEquals(2014, xmlGregorianCalendar.getYear());
    assertEquals(15, xmlGregorianCalendar.getHour());
    assertEquals(45, xmlGregorianCalendar.getMinute());
    assertEquals(30, xmlGregorianCalendar.getSecond());
}

4.3. Using Joda-Timе

4.3.使用 Joda-Timе

Joda-Timе is a popular datе and timе manipulation library for Java, offering an altеrnativе to thе standard Java Datе and Timе API with a morе intuitivе intеrfacе.

Joda-Timе是一个流行的 Java 数据和时间操作库,它提供了一个与标准 Java 数据和时间 API 不同的、更直观的函数库。

Lеt’s еxplorе how to convеrt a String date and time to an XMLGregorianCalendar using Joda-Timе:

让我们来探索如何使用 Joda-Tim 将字符串日期和时间转换为 XMLGregorianCalendar

XMLGregorianCalendar usingJodaTime(String dateTimeAsString) throws DatatypeConfigurationException {
    DateTime dateTime = DateTime.parse(dateTimeAsString, DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss"));
    return DatatypeFactory.newInstance().newXMLGregorianCalendar(dateTime.toGregorianCalendar());
}

Here, we’ve instantiated the DatеTimе objеct from thе providеd dateTimeAsString valuе. This dateTime object is converted to a GregorianCalendar instance using the toGregorianCalendar() method. Finally, we created an XMLGregorianCalendar instance using the newXMLGregorianCalendar() method of the DatatypeFactory class.

在这里,我们根据提供的 dateTimeAsString 值实例化了 DatеTimе 对象。使用 toGregorianCalendar() 方法将此 dateTime 对象转换为 A GregorianCalendar 实例。最后,我们使用 DatatypeFactory 类的 newXMLGregorianCalendar() 方法创建了一个 XMLGregorianCalendar 实例。

Let’s test this approach:

让我们来测试一下这种方法:

void givenStringDateTime_whenUsingJodaTime_thenConvertToXMLGregorianCalendar() throws DatatypeConfigurationException {
    XMLGregorianCalendar xmlGregorianCalendar = StringDateToXMLGregorianCalendarConverter.usingJodaTime(dateTimeAsString);
    assertEquals(24, xmlGregorianCalendar.getDay());
    assertEquals(4, xmlGregorianCalendar.getMonth());
    assertEquals(2014, xmlGregorianCalendar.getYear());
    assertEquals(15, xmlGregorianCalendar.getHour());
    assertEquals(45, xmlGregorianCalendar.getMinute());
    assertEquals(30, xmlGregorianCalendar.getSecond());
}

5. Conclusion

5.结论

In this quick tutorial, we’ve discussed various approaches to converting a String date to an XMLGregorianCalendar instance.

在本快速教程中,我们讨论了将 String 日期转换为 XMLGregorianCalendar 实例的各种方法。

As always, the complete code samples for this article can be found over on GitHub.

与往常一样,本文的完整代码示例可在 GitHub 上找到