Conversions { @SuppressWarnings("deprecation") protected static LocalDate toLocalDate(Object obj) { if (obj == null) { return null; } if (obj instanceof LocalDate) { return (LocalDate) obj; } if (obj instanceof LocalDateTime) { return ((LocalDateTime) obj).toLocalDate(); } if (obj instanceof java.sql.Date) { return ((java.sql.Date) obj).toLocalDate(); } if (obj instanceof java.sql.Time) { throw new IllegalArgumentException("Unable to convert to LocalDate from a java.sql.Time value '" + obj + "'"); } if (obj instanceof java.util.Date) { java.util.Date date = (java.util.Date) obj; return LocalDate.of(date.getYear() + 1900, date.getMonth() + 1, date.getDate()); } if (obj instanceof Long) { return LocalDate.ofEpochDay((Long) obj); } if (obj instanceof Integer) { return LocalDate.ofEpochDay((Integer) obj); } throw new IllegalArgumentException("Unable to convert to LocalDate from unexpected value '" + obj + "' of type " + obj.getClass().getName()); } private  Conversions(); static long toEpochMicros(Instant instant); static Instant toInstantFromMicros(long microsSinceEpoch);  }
@Test public void shouldReturnSameLocalDateInstanceWhenConvertingToLocalDate() { LocalDate now = LocalDate.now(); assertThat(Conversions.toLocalDate(now)).isSameAs(now); }
@Test public void shouldReturnLocalDateInstanceWhenConvertingLocalDateTimeToLocalDate() { LocalDateTime now = LocalDateTime.now(); assertThat(Conversions.toLocalDate(now)).isEqualTo(now.toLocalDate()); }
@SuppressWarnings("deprecation") @Test public void shouldReturnLocalDateInstanceWhenConvertingUtilDateToLocalDate() { LocalDate now = LocalDate.now(); java.util.Date date = new java.util.Date(now.getYear() - 1900, now.getMonthValue() - 1, now.getDayOfMonth()); assertThat(Conversions.toLocalDate(date)).isEqualTo(now); }
@SuppressWarnings("deprecation") @Test public void shouldReturnLocalDateInstanceWhenConvertingSqlDateToLocalDate() { LocalDate now = LocalDate.now(); java.sql.Date date = new java.sql.Date(now.getYear() - 1900, now.getMonthValue() - 1, now.getDayOfMonth()); assertThat(Conversions.toLocalDate(date)).isEqualTo(now); }
@Test public void shouldThrowExceptionWhenConvertingSqlTimeToLocalDate() { java.sql.Time time = new java.sql.Time(1); try { Conversions.toLocalDate(time); fail("Should not accept java.sql.Time values"); } catch (IllegalArgumentException e) { } }
@Test public void shouldReturnLocalDateInstanceWhenConvertingLongToLocalDate() { LocalDate now = LocalDate.now(); long epochDay = now.toEpochDay(); assertThat(Conversions.toLocalDate(epochDay)).isEqualTo(now); }