use of java.time.Instant in project hibernate-orm by hibernate.
the class LocalTimeJavaDescriptor method unwrap.
@Override
@SuppressWarnings("unchecked")
public <X> X unwrap(LocalTime value, Class<X> type, WrapperOptions options) {
if (value == null) {
return null;
}
if (LocalDate.class.isAssignableFrom(type)) {
return (X) value;
}
if (Time.class.isAssignableFrom(type)) {
return (X) Time.valueOf(value);
}
// Oracle documentation says to set the Date to January 1, 1970 when convert from
// a LocalTime to a Calendar. IMO the same should hold true for converting to all
// the legacy Date/Time types...
final ZonedDateTime zonedDateTime = value.atDate(LocalDate.of(1970, 1, 1)).atZone(ZoneId.systemDefault());
if (Calendar.class.isAssignableFrom(type)) {
return (X) GregorianCalendar.from(zonedDateTime);
}
final Instant instant = zonedDateTime.toInstant();
if (Timestamp.class.isAssignableFrom(type)) {
return (X) Timestamp.from(instant);
}
if (Date.class.equals(type)) {
return (X) Date.from(instant);
}
if (Long.class.isAssignableFrom(type)) {
return (X) Long.valueOf(instant.toEpochMilli());
}
throw unknownUnwrap(type);
}
use of java.time.Instant in project hibernate-orm by hibernate.
the class OffsetTimeJavaDescriptor method unwrap.
@Override
@SuppressWarnings("unchecked")
public <X> X unwrap(OffsetTime offsetTime, Class<X> type, WrapperOptions options) {
if (offsetTime == null) {
return null;
}
if (OffsetTime.class.isAssignableFrom(type)) {
return (X) offsetTime;
}
if (java.sql.Time.class.isAssignableFrom(type)) {
return (X) java.sql.Time.valueOf(offsetTime.toLocalTime());
}
final ZonedDateTime zonedDateTime = offsetTime.atDate(LocalDate.of(1970, 1, 1)).toZonedDateTime();
if (Timestamp.class.isAssignableFrom(type)) {
return (X) Timestamp.valueOf(zonedDateTime.toLocalDateTime());
}
if (Calendar.class.isAssignableFrom(type)) {
return (X) GregorianCalendar.from(zonedDateTime);
}
final Instant instant = zonedDateTime.toInstant();
if (Long.class.isAssignableFrom(type)) {
return (X) Long.valueOf(instant.toEpochMilli());
}
if (java.util.Date.class.isAssignableFrom(type)) {
return (X) java.util.Date.from(instant);
}
throw unknownUnwrap(type);
}
use of java.time.Instant in project querydsl by querydsl.
the class JSR310InstantTypeTest method set.
@Test
public void set() throws SQLException {
Instant value = Instant.now();
Timestamp ts = new Timestamp(value.toEpochMilli());
PreparedStatement stmt = EasyMock.createNiceMock(PreparedStatement.class);
stmt.setTimestamp(1, ts, UTC);
EasyMock.replay(stmt);
type.setValue(stmt, 1, value);
EasyMock.verify(stmt);
}
use of java.time.Instant in project querydsl by querydsl.
the class JSR310InstantTypeTest method jodaSet.
@Test
public void jodaSet() throws SQLException {
Instant value = Instant.now();
Timestamp ts = new Timestamp(value.toEpochMilli());
PreparedStatement stmt = EasyMock.createNiceMock(PreparedStatement.class);
stmt.setTimestamp(1, ts);
EasyMock.replay(stmt);
new DateTimeType().setValue(stmt, 1, toJoda(value));
EasyMock.verify(stmt);
}
use of java.time.Instant in project dropwizard by dropwizard.
the class InstantMapperTest method mapColumnByName_TimestampIsNull.
@Test
public void mapColumnByName_TimestampIsNull() throws Exception {
when(resultSet.getTimestamp("instant")).thenReturn(null);
Instant actual = new InstantMapper().mapColumn(resultSet, "instant", null);
assertThat(actual).isNull();
}
Aggregations