Conversions { @SuppressWarnings("deprecation") protected static LocalDateTime toLocalDateTime(Object obj) { if (obj == null) { return null; } if (obj instanceof OffsetDateTime) { return ((OffsetDateTime) obj).toLocalDateTime(); } if (obj instanceof Instant) { return ((Instant) obj).atOffset(ZoneOffset.UTC).toLocalDateTime(); } if (obj instanceof LocalDateTime) { return (LocalDateTime) obj; } if (obj instanceof LocalDate) { LocalDate date = (LocalDate) obj; return LocalDateTime.of(date, LocalTime.MIDNIGHT); } if (obj instanceof LocalTime) { LocalTime time = (LocalTime) obj; return LocalDateTime.of(EPOCH, time); } if (obj instanceof java.sql.Date) { java.sql.Date sqlDate = (java.sql.Date) obj; LocalDate date = sqlDate.toLocalDate(); return LocalDateTime.of(date, LocalTime.MIDNIGHT); } if (obj instanceof java.sql.Time) { LocalTime localTime = toLocalTime(obj); return LocalDateTime.of(EPOCH, localTime); } if (obj instanceof java.sql.Timestamp) { java.sql.Timestamp timestamp = (java.sql.Timestamp) obj; return LocalDateTime.of(timestamp.getYear() + 1900, timestamp.getMonth() + 1, timestamp.getDate(), timestamp.getHours(), timestamp.getMinutes(), timestamp.getSeconds(), timestamp.getNanos()); } if (obj instanceof java.util.Date) { java.util.Date date = (java.util.Date) obj; long millis = (int) (date.getTime() % Conversions.MILLISECONDS_PER_SECOND); if (millis < 0) { millis = Conversions.MILLISECONDS_PER_SECOND + millis; } int nanosOfSecond = (int) (millis * Conversions.NANOSECONDS_PER_MILLISECOND); return LocalDateTime.of(date.getYear() + 1900, date.getMonth() + 1, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), nanosOfSecond); } throw new IllegalArgumentException("Unable to convert to LocalTime from unexpected value '" + obj + "' of type " + obj.getClass().getName()); } private  Conversions(); static long toEpochMicros(Instant instant); static Instant toInstantFromMicros(long microsSinceEpoch);  }
@Test public void shouldReturnSameLocalDateTimeInstanceWhenConvertingToLocalDateTime() { LocalDateTime now = LocalDateTime.now(); assertThat(Conversions.toLocalDateTime(now)).isSameAs(now); }
@Test public void shouldReturnLocalDateTimeInstanceWhenConvertingLocalDateToLocalDateTime() { LocalDate now = LocalDate.now(); assertThat(Conversions.toLocalDateTime(now)).isEqualTo(LocalDateTime.of(now, LocalTime.MIDNIGHT)); }
@Test public void shouldReturnLocalDateTimeInstanceWhenConvertingLocalTimeToLocalDateTime() { LocalTime now = LocalTime.now(); assertThat(Conversions.toLocalDateTime(now)).isEqualTo(LocalDateTime.of(Conversions.EPOCH, now)); }
@SuppressWarnings("deprecation") @Test public void shouldReturnLocalDateTimeInstanceWhenConvertingUtilTimeToLocalDateTime() { LocalDateTime now = LocalDateTime.now(); java.util.Date date = new java.util.Date(now.getYear() - 1900, now.getMonthValue() - 1, now.getDayOfMonth(), now.getHour(), now.getMinute(), now.getSecond()); assertThat(Conversions.toLocalDateTime(date)).isEqualTo(now.withNano(0)); }
@SuppressWarnings("deprecation") @Test public void shouldReturnLocalDateTimeInstanceWhenConvertingSqlDateToLocalDateTime() { LocalDate now = LocalDate.now(); java.sql.Date date = new java.sql.Date(now.getYear() - 1900, now.getMonthValue() - 1, now.getDayOfMonth()); assertThat(Conversions.toLocalDateTime(date)).isEqualTo(LocalDateTime.of(now, LocalTime.MIDNIGHT)); }
@SuppressWarnings("deprecation") @Test public void shouldReturnLocalDateTimeInstanceWhenConvertingSqlTimeToLocalDateTime() { LocalTime now = LocalTime.now(); java.sql.Time time = new java.sql.Time(now.getHour(), now.getMinute(), now.getSecond()); assertThat(Conversions.toLocalDateTime(time)).isEqualTo(LocalDateTime.of(Conversions.EPOCH, now.withNano(0))); }
@Test public void shouldReturnLocalDateTimeInstanceWhenConvertingLongToLocalDateTime() { try { Conversions.toLocalDateTime(Long.valueOf(1)); fail("Should not accept Long values"); } catch (IllegalArgumentException e) { } }