How to check if a Calendar instance was originally a wrong date

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 to know if the original xsd datetime was valid (month < 12, day < 31, 30, 29, 28 according to month ...ect) or not. Example: this is a valid date: 2015-07-30T09:32:05.543+02:00 and this is not: 2015-07-35T09:32:05.543+02:00

I tried to use setLenient on the instance in the web service, but it doesn't seem to raise an exception when the original date was wrong.

Is there any way to do it? My guess is to tell JAXB to do it the right way in the global.jaxb file, here's the one i use right now:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema"
               jaxb:extensionBindingPrefixes="xjc">
    <jaxb:globalBindings>
        <xjc:simple/>
        <xjc:serializable uid="-1"/>
        <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
                       parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
                       printMethod="javax.xml.bind.DatatypeConverter.printDateTime"/>
    </jaxb:globalBindings>
</jaxb:bindings>

Any help would be appreciated. Thanks.


tl;dr

OffsetDateTime.parse( "2015-07-35T09:32:05.543+02:00" ) 
    … catch ( DateTimeParseException e )

java.time

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


About java.time

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?

  • Java SE 8 , Java SE 9 , and later
  • Built-in.
  • Part of the standard Java API with a bundled implementation.
  • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
  • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport .
  • Android
  • Later versions of Android bundle implementations of the java.time classes.
  • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use 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.


    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>
    
    链接地址: http://www.djcxy.com/p/3100.html

    上一篇: GregorianCalendar上的内存泄漏

    下一篇: 如何检查日历实例最初是否是错误的日期