I have a Calendar instance, parsed from XSD datetime via the javax.xml.bind.DatatypeConverter.parseDateTime method by JAXB.
At runtime, i\'m in a web service, and i want
I solved my problem by using JodaTime's ISODateTimeFormat#dateTimeParser() and custom Adapter in a custom JAXB bindings file. JodaTime is much better API than the Calendar standard Java API, and it is non lenient by default.
I could have done the same for Calendar, but i should have defined all the formats myself, where JodaTime defines them already.
Here a code sample i made (it answers too to my other question: Check if xsd datetime had a defined timezone before conversion to Java object)
package com.app.adapter;
public class MyDatetimeAdapter extends XmlAdapter<String, DateTime> {
private final static DateTimeFormatter DATETIME_FORMATTER = ISODateTimeFormat.dateTime();
private final static Pattern TIMEZONE_PATTERN = Pattern.compile("(\\+|\\-)[0-9]{2}:[0-9]{2}$");
@Override
public DateTime unmarshal(String string) throws Exception {
string = string.trim();
try {
DateTime tmp = ISODateTimeFormat.dateTimeParser().parseDateTime(string);
if (string.charAt(string.length()-1) == 'Z' || TIMEZONE_PATTERN.matcher(string).find()) {
return new CustomDateTime(tmp);
}
return new CustomDateTime(tmp, CustomDateTime.Error.NO_TIMEZONE);
} catch (IllegalArgumentException e) {
return new CustomDateTime(null, CustomDateTime.Error.INVALID);
}
}
@Override
public String marshal(CustomDateTime date) throws Exception {
return DATETIME_FORMATTER.print(date.getDateTime());
}
}
And here's CustomDateTime (a POJO that wraps a JodaTime's DateTime and an error code)
package com.app.types;
public final class CustomDateTime {
private DateTime dateTime;
private Error error;
// getter .. setters .. constructor
public static CustomDateTime getInstance(Error error) {
return new CustomDateTime(DateTime.now(), error);
}
public static CustomDateTime getInstance() {
return getInstance(Error.NONE);
}
public enum Error {
NONE,
NO_TIMEZONE
INVALID;
}
}
And finally, the JAXB binding file.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.1">
<jaxb:globalBindings>
<xjc:javaType adapter="com.app.adapter.MyDatetimeAdapter"
name="com.app.types.CustomDateTime" xmlType="xs:dateTime"/>
</jaxb:globalBindings>
</jaxb:bindings>
OffsetDateTime.parse( "2015-07-35T09:32:05.543+02:00" )
… catch ( DateTimeParseException e )
The troublesome old date-time classes such as java.util.Date, java.util.Calendar, and java.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 & Java 9.
Likewise, the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.
OffsetDateTime
& DateTimeParseException
To determine invalid date-time string, attempt to parse and trap for exception. Given that your inputs have an offset-from-UTC but not a time zone, parse as OffsetDateTime objects. Invalid inputs throw DateTimeParseException.
String inputGood = "2015-07-30T09:32:05.543+02:00" ;
String inputBad = "2015-07-35T09:32:05.543+02:00" ;
try{
// Good
OffsetDateTime odtGood = OffsetDateTime.parse( inputGood ) ;
System.out.println( "odtGood.toString(): " + odtGood ) ;
// Bad
OffsetDateTime odtBad = OffsetDateTime.parse( inputBad ) ;
System.out.println( "odtBad.toString(): " + odtBad ) ;
} catch ( DateTimeParseException e ) {
System.out.println( e ) ;
}
See code run live at IdeOne.com.
odtGood.toString(): 2015-07-30T09:32:05.543+02:00
java.time.format.DateTimeParseException: Text '2015-07-35T09:32:05.543+02:00' could not be parsed: Invalid value for DayOfMonth (valid values 1 - 28/31): 35
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
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.