我有一个将ISO日期格式转换为XMLGregorianCalendar的util方法。
我正在尝试为它编写单元测试用例,但是,我无法创建与实际对象相同的预期对象 - 两者之间发生时区转换且对象不匹配。
public static XMLGregorianCalendar convertStringToXMLGregorianCalendar(final String dateStrInXMLGregorianCalendar) {
try {
DateTime dateTime = ISODateTimeFormat.dateTime().parseDateTime(dateStrInXMLGregorianCalendar);
GregorianCalendar gregCal = new GregorianCalendar(dateTime.getZone().toTimeZone());
gregCal.setTimeInMillis(dateTime.getMillis());
return DatatypeFactory.newInstance().newXMLGregorianCalendar(gregCal);
} catch (Exception e) {
throw new RuntimeException(String.format("Exception while converting %s to XMLGregorianCalendar!", dateStrInXMLGregorianCalendar), e);
}
}
JUnit的:
@Test
public void testFoo() throws Exception {
XMLGregorianCalendar result = convertStringToXMLGregorianCalendar("2016-02-06T00:00:00.000+0100");
assertNotNull(result);
assertTrue(result.toGregorianCalendar().toZonedDateTime().toLocalDateTime().compareTo(
DatatypeFactory.newInstance().newXMLGregorianCalendar(2016, 2, 6, 0, 0, 0, 0, 0).toGregorianCalendar().toZonedDateTime().toLocalDateTime()) == 0);
}