RegistrationDateAdapter extends XmlAdapter<String, XMLGregorianCalendar> { @Override public XMLGregorianCalendar unmarshal(String datetime) throws Exception { try { final DateTimeParser[] parsers = { DateTimeFormat.forPattern("yyyy-MM-dd").getParser(), DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm").getParser(), DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mmZ").getParser(), DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").getParser(), DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").getParser(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").getParser(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ssZ").getParser() }; DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder().append(null, parsers).toFormatter(); DateTime dateTime = dateTimeFormatter.withZoneUTC().parseDateTime(datetime.trim()); return DatatypeFactory.newInstance().newXMLGregorianCalendar(dateTime.toGregorianCalendar()); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Valid date or date and time format must be used when specifying values for start/end registration dates."); } }  @Override XMLGregorianCalendar unmarshal(String datetime); @Override String marshal(XMLGregorianCalendar xmlGregorianCalendar);  }
@Test public void unmarshalAllAllowedFormatsTest() throws Exception { RegistrationDateAdapter adapter = new RegistrationDateAdapter(); XMLGregorianCalendar calendar = adapter.unmarshal("2020-02-02"); compareCalendarWithExpectedValues(calendar, 2020, 2, 2, 0, 0, 0); calendar = adapter.unmarshal("2015-12-25T00:01"); compareCalendarWithExpectedValues(calendar, 2015, 12, 25, 0, 1, 0); calendar = adapter.unmarshal("2015-12-25T00:01+04:00"); compareCalendarWithExpectedValues(calendar, 2015, 12, 24, 20, 1, 0); calendar = adapter.unmarshal("2018-02-03T10:00:02"); compareCalendarWithExpectedValues(calendar, 2018, 2, 3, 10, 0, 2); calendar = adapter.unmarshal("2018-02-03T10:00:02+04:00"); compareCalendarWithExpectedValues(calendar, 2018, 2, 3, 6, 0, 2); calendar = adapter.unmarshal("2018-02-03 10:00:02+05:30"); compareCalendarWithExpectedValues(calendar, 2018, 2, 3, 4, 30, 2); calendar = adapter.unmarshal("2018-03-03 00:20:02"); compareCalendarWithExpectedValues(calendar, 2018, 3, 3, 0, 20, 2); }
@Test public void unmarshalIllegalFormatTest() { RegistrationDateAdapter adapter = new RegistrationDateAdapter(); try { adapter.unmarshal("2020-02"); } catch (Exception e) { assertTrue(e.getMessage().contains("Valid date or date and time format must be used when specifying values for start/end registration dates.")); } try { adapter.unmarshal("2020-02-02T12:02AM"); } catch (Exception e) { assertTrue(e.getMessage().contains("Valid date or date and time format must be used when specifying values for start/end registration dates.")); } }
@Test public void unmarshalIllegalFormatTest() { RegistrationDateAdapter adapter = new RegistrationDateAdapter(); try { adapter.unmarshal("2020-02"); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); assertTrue(e.getMessage().contains("Valid date or date and time format must be used when specifying values for start/end registration dates.")); } try { adapter.unmarshal("2020-02-02T12:02AM"); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); assertTrue(e.getMessage().contains("Valid date or date and time format must be used when specifying values for start/end registration dates.")); } }