Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 3522

Convert String Date to XMLGregorianCalendar in Java

$
0
0

1. Overview

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

2. XMLGregorianCalendar

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.

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.

3. String Date to 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.

3.1. Using Standard DatatypeFactory

Here’s an example using DatatypeFactory for parsing date strings into 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.

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

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

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:

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

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.

4.1. Using 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:

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.

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

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:

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:

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е

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е.

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

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.

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

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

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

       

Viewing all articles
Browse latest Browse all 3522

Trending Articles