Skip to content

Instantly share code, notes, and snippets.

@novoj
Created December 18, 2014 07:40
Show Gist options
  • Save novoj/6d6d99229e8c50763f2a to your computer and use it in GitHub Desktop.
Save novoj/6d6d99229e8c50763f2a to your computer and use it in GitHub Desktop.
public interface DateConversionHelper {
static LocalDateTime fromXmlDate(XMLGregorianCalendar calendar) {
if (calendar == null) {
return null;
}
final int year = calendar.getYear() > 0 ? calendar.getYear() : 0;
final int month = calendar.getMonth();
final int day = calendar.getDay();
final int hour = calendar.getHour() > 0 ? calendar.getHour() : 0;
final int minute = calendar.getMinute() > 0 ? calendar.getMinute() : 0;
final int second = calendar.getSecond() > 0 ? calendar.getSecond() : 0;
final int millisecond = calendar.getMillisecond() > 0 ? calendar.getMillisecond() : 0;
final ZoneOffset offset = LocalDateTime.now().atZone(ZoneId.of("UTC")).withZoneSameLocal(ZoneId.systemDefault()).toOffsetDateTime().getOffset();
return LocalDateTime.of(year, month, day, hour, minute, second, millisecond).plusSeconds(offset.getTotalSeconds());
}
static XMLGregorianCalendar toXmlDate(LocalDate value) {
if (value == null) {
return null;
}
try {
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.setTime(value);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
} catch (DatatypeConfigurationException ex) {
//WTF ENTERPRISE PIECE OF SHIT
throw new RuntimeException(ex);
}
}
static XMLGregorianCalendar toXmlDate(LocalDateTime value) {
if (value == null) {
return null;
}
try {
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.setTime(value);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
} catch (DatatypeConfigurationException ex) {
throw new RuntimeException(ex);
}
}
static XMLGregorianCalendar toXmlDate(Date value) {
if (value == null) {
return null;
}
try {
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.setTime(value);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
} catch (DatatypeConfigurationException ex) {
throw new RuntimeException(ex);
}
}
}
public class DateConversionHelperTest {
@Test
public void shouldConvertUtcCalendarToLocalDateTime() throws Exception {
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
calendar.setTime(new GregorianCalendar(2014, 0, 1, 0, 0, 0).getTime());
final XMLGregorianCalendar xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
final LocalDateTime date = DateConversionHelper.fromXmlDate(xmlCalendar);
assertEquals("2014-01-01T00:00", date.toString());
}
@Test
public void shouldConvertDateTimeToUtcForm() throws Exception {
final XMLGregorianCalendar calendar = DateConversionHelper.toXmlDate(LocalDateTime.of(2014, 1, 1, 0, 0, 0));
assertEquals("2013-12-31T23:00:00.000Z", calendar.toString());
}
@Test
public void shouldConvertDateToUtcForm() throws Exception {
final XMLGregorianCalendar calendar = DateConversionHelper.toXmlDate(LocalDate.of(2014, 1, 1));
assertEquals("2013-12-31T23:00:00.000Z", calendar.toString());
}
@Test
public void shouldConvertOldDateToUtcForm() throws Exception {
final XMLGregorianCalendar calendar = DateConversionHelper.toXmlDate(new GregorianCalendar(2014, 0, 1, 0, 0, 0).getTime());
assertEquals("2013-12-31T23:00:00.000Z", calendar.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment