What's the best way to parse an XML dateTime in Java? Legal dateTime values include 2002-10-10T12:00:00-05:00 AND 2002-10-10T17:00:00Z
Is there a good open source library I can use, or should I roll my own using SimpleDateFormat or similar?
What's the best way to parse an XML dateTime in Java? Legal dateTime values include 2002-10-10T12:00:00-05:00 AND 2002-10-10T17:00:00Z
Is there a good open source library I can use, or should I roll my own using SimpleDateFormat or similar?
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.)
Ideally, XML processing packages that are schema-aware (or to be used as basis for things that are) should provide accessors for typed content. I know of one (http://woodstox.codehaus.org/), but it does not (yet) offer access to date/time, just simpler types (numeric, arrays, QNames etc). There is a request to support javax.xml.datatype.XMLGregorianCalendar.
Alas, not many do. However, it you are using specific package (like XOM or JDOM etc), it might not be a bad idea to ask this question on their user list.
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
http://xmlbeans.apache.org/samples/DateTime.html
There is XmlDateTime class. Just do XMLDateTime.stringToDate(xmlDateTime).