Conversions { @SuppressWarnings("deprecation") protected static LocalTime toLocalTime(Object obj) { if (obj == null) { return null; } if (obj instanceof LocalTime) { return (LocalTime) obj; } if (obj instanceof LocalDateTime) { return ((LocalDateTime) obj).toLocalTime(); } if (obj instanceof java.sql.Date) { throw new IllegalArgumentException("Unable to convert to LocalDate from a java.sql.Date value '" + obj + "'"); } if (obj instanceof java.sql.Time) { java.sql.Time time = (java.sql.Time) obj; long millis = (int) (time.getTime() % Conversions.MILLISECONDS_PER_SECOND); int nanosOfSecond = (int) (millis * Conversions.NANOSECONDS_PER_MILLISECOND); return LocalTime.of(time.getHours(), time.getMinutes(), time.getSeconds(), nanosOfSecond); } if (obj instanceof java.sql.Timestamp) { java.sql.Timestamp timestamp = (java.sql.Timestamp) obj; return LocalTime.of(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); int nanosOfSecond = (int) (millis * Conversions.NANOSECONDS_PER_MILLISECOND); return LocalTime.of(date.getHours(), date.getMinutes(), date.getSeconds(), nanosOfSecond); } if (obj instanceof Duration) { Long value = ((Duration) obj).toNanos(); if (value >= 0 && value <= NANOSECONDS_PER_DAY) { return LocalTime.ofNanoOfDay(value); } else { throw new IllegalArgumentException("Time values must use number of milliseconds greater than 0 and less than 86400000000000"); } } 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 shouldReturnSameLocalTimeInstanceWhenConvertingToLocalTime() { LocalTime now = LocalTime.now(); assertThat(Conversions.toLocalTime(now)).isSameAs(now); }
@Test public void shouldReturnLocalTimeInstanceWhenConvertingLocalDateTimeToLocalTime() { LocalDateTime now = LocalDateTime.now(); assertThat(Conversions.toLocalTime(now)).isEqualTo(now.toLocalTime()); }
@SuppressWarnings("deprecation") @Test public void shouldReturnLocalTimeInstanceWhenConvertingUtilTimeToLocalTime() { LocalTime now = LocalTime.now(); java.util.Date date = new java.util.Date(0, 0, 1, now.getHour(), now.getMinute(), now.getSecond()); assertThat(Conversions.toLocalTime(date)).isEqualTo(now.withNano(0)); }
@Test public void shouldThrowExceptionWhenConvertingSqlDateToLocalTime() { java.sql.Date date = new java.sql.Date(1); try { Conversions.toLocalTime(date); fail("Should not accept java.sql.Date values"); } catch (IllegalArgumentException e) { } }
@SuppressWarnings("deprecation") @Test public void shouldReturnLocalTimeInstanceWhenConvertingSqlTimeToLocalTime() { LocalTime now = LocalTime.now(); java.sql.Time time = new java.sql.Time(now.getHour(), now.getMinute(), now.getSecond()); assertThat(Conversions.toLocalTime(time)).isEqualTo(now.withNano(0)); }