Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to parse an XML dateTime in Java?

Tags:

java

xml

People also ask

What is the best way to parse XML in Java?

Java XML Parser - DOM DOM Parser is the easiest java xml parser to learn. DOM parser loads the XML file into memory and we can traverse it node by node to parse the XML. DOM Parser is good for small files but when file size increases it performs slow and consumes more memory.

What is the DateTime format in XML?

The dateTime is specified in the following form "YYYY-MM-DDThh:mm:ss" where: YYYY indicates the year. MM indicates the month. DD indicates the day.

What is parsing in Java XML?

XML Parsing refers to going through an XML document in order to access or modify data.


There's also javax.xml.bind.DatatypeConverter#parseDateTime(String xsdDateTime), which comes as part of the JDK.


I think you want ISODateTimeFormat.dateTimeNoMillis() from Joda Time. In general I would strongly urge you to stay away from the built-in Date/Calendar classes in Java. Joda Time is much better designed, favours immutability (in particular the formatters are immutable and thread-safe) and is the basis for the new date/time API in Java 7.

Sample code:

import org.joda.time.*;
import org.joda.time.format.*;

class Test
{   
    public static void main(String[] args)
    {
        parse("2002-10-10T12:00:00-05:00");
        parse("2002-10-10T17:00:00Z");
    }

    private static final DateTimeFormatter XML_DATE_TIME_FORMAT =
        ISODateTimeFormat.dateTimeNoMillis();

    private static final DateTimeFormatter CHECKING_FORMAT =
        ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);

    static void parse(String text)
    {
        System.out.println("Parsing: " + text);
        DateTime dt = XML_DATE_TIME_FORMAT.parseDateTime(text);
        System.out.println("Parsed to: " + CHECKING_FORMAT.print(dt));
    }
}

Output:

Parsing: 2002-10-10T12:00:00-05:00
Parsed to: 2002-10-10T17:00:00.000Z
Parsing: 2002-10-10T17:00:00Z
Parsed to: 2002-10-10T17:00:00.000Z

(Note that in the output both end up as the same UTC time. The output formatted uses UTC because we asked it to with the withZone call.)


StaxMan is absolutely correct. In order to use SimpleDateFormat, you need to turn off lax parsing in each SimpleDateFormat and iterate over several SimpleDateFormat formats until you find the one that parses the date without throwing an exception. If you leave lax parsing on, you are prone to get a match when you didn't really want one, and the lexical space of XSD:DateTime leaves some flexibility in format that SimpleDateFormat can't handle in a single expression.

XML Schema 1.0 does indeed use ISO 8601, which Joda Time, as suggested by Jon Skeet, implements so that is a valid option.

If you want to keep it all in the native Java packages, you can also use XMLGregorianCalendar in conjunction with DatatypeFactory to parse and create XSD:Datetime strings.

See DatatypeFactory.newXMLGregorianCalendar and XMLGregorianCalendar.toXMLFormat


tl;dr

Instant instant = Instant.parse( "2002-10-10T17:00:00Z" );
OffsetDateTime odt = OffsetDateTime.parse( "2002-10-10T12:00:00-05:00" );

Details

The other Answers are correct but now outdated. They use troublesome old classes now supplanted by the java.time framework.

No such thing as an “XML dateTime”. XML does not define any data type beyond text.

Using java.time

The input string happens to comply with ISO 8601 standard formatting. So no need to specify a formatting pattern as the java.time classes use ISO 8601 by default when parsing/generating strings.

Instant

The second input string ends in a Z, short for Zulu, and means UTC.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds.

String input = "2002-10-10T17:00:00Z":
Instant instant = Instant.parse( input );

OffsetDateTime

The first input string includes an offset-from-UTC, so we parse as an OffsetDateTime.

String input = "2002-10-10T12:00:00-05:00" ;
OffsetDateTime odt = OffsetDateTime.parse( input );

ZonedDateTime

If you have a specific time zone in mind, rather than a mere offset-from-UTC, apply that.

Use a proper time zone name in format of continent/region. Never use the 3-4 letter abbreviations that are not true time zones, not standardized, and not even unique(!).

ZoneId zoneId = ZoneId.of( "America/Cancun" );
ZonedDateTime zdt = odt.atZone( zoneId );

enter image description here

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


See Parse and format dateTime values, although: -It takes "GMT" as the default timezone -It does not complain if there are trailing non-parseable parts -Does not take into account that TimeZone defaults to "GMT" on wrong "GMT+xxxx"


http://xmlbeans.apache.org/samples/DateTime.html

There is XmlDateTime class. Just do XMLDateTime.stringToDate(xmlDateTime).