#449 Handle XmlGregoriancalendar from String with empty date format as xml datatype

This commit is contained in:
sjaakd 2015-02-05 20:19:21 +01:00
parent 0ded169ecb
commit 83ace655e1
2 changed files with 18 additions and 7 deletions

View File

@ -24,11 +24,16 @@ private XMLGregorianCalendar ${name}( String date, String dateFormat ) {
}
try {
DateFormat df = dateFormat != null ? new SimpleDateFormat( dateFormat ) : SimpleDateFormat.getInstance();
if ( dateFormat != null ) {
DateFormat df = new SimpleDateFormat( dateFormat );
GregorianCalendar c = new GregorianCalendar();
c.setTime( df.parse( date ) );
return DatatypeFactory.newInstance().newXMLGregorianCalendar( c );
}
else {
return DatatypeFactory.newInstance().newXMLGregorianCalendar( date );
}
}
catch ( DatatypeConfigurationException ex ) {
throw new RuntimeException( ex );
}

View File

@ -165,12 +165,18 @@ public class BuiltInTest {
assertThat( target.getProp() ).isNotNull();
assertThat( target.getProp().toString() ).isEqualTo( "1999-07-05T00:00:00.000+02:00" );
source.setProp( createLocaleDate( "31-08-1982 10:20:56" ) );
// direct,via lexical representation
source.setProp( "2000-03-04T23:00:00+03:00" );
target = StringToXmlGregCalMapper.INSTANCE.map( source );
assertThat( target ).isNotNull();
assertThat( target.getProp() ).isNotNull();
assertThat( target.getProp().toString() ).isEqualTo( "1982-08-31T10:20:00.000+02:00" );
assertThat( target.getProp().toString() ).isEqualTo( "2000-03-04T23:00:00+03:00" );
// null string
source.setProp( null );
target = StringToXmlGregCalMapper.INSTANCE.map( source );
assertThat( target ).isNotNull();
assertThat( target.getProp() ).isNull();
}