xml - java - How to check if a Calendar instance was originally a wrong date -
i have calendar instance, parsed xsd datetime via javax.xml.bind.datatypeconverter.parsedatetime
method jaxb.
at runtime, i'm in web service, , want know if original xsd datetime valid (month < 12, day < 31, 30, 29, 28 according month ...ect) or not. example: valid date: 2015-07-30t09:32:05.543+02:00
, not: 2015-07-35t09:32:05.543+02:00
i tried use setlenient
on instance in web service, doesn't seem raise exception when original date wrong.
is there way it? guess tell jaxb right way in global.jaxb
file, here's 1 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 appreciated. thanks.
i solved problem using jodatime's isodatetimeformat#datetimeparser()
, custom adapter in custom jaxb bindings file. jodatime better api calendar standard java api, , non lenient default.
i have done same calendar, should have defined formats myself, jodatime defines them already.
here code sample made (it answers other question: check if xsd datetime had defined timezone before conversion 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 wraps jodatime's datetime , 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, 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>
Comments
Post a Comment