mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#449 Handle XmlGregoriancalendar from String with empty date format as xml datatype
This commit is contained in:
parent
0ded169ecb
commit
83ace655e1
@ -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 );
|
||||
}
|
||||
|
@ -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();
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user