Search in sources :

Example 31 with Instant

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);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Instant(java.time.Instant)

Example 32 with Instant

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);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Instant(java.time.Instant)

Example 33 with Instant

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);
}
Also used : Instant(java.time.Instant) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 34 with Instant

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);
}
Also used : Instant(java.time.Instant) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 35 with Instant

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();
}
Also used : Instant(java.time.Instant) Test(org.junit.Test)

Aggregations

Instant (java.time.Instant)463 Test (org.testng.annotations.Test)143 Test (org.junit.Test)85 ZonedDateTime (java.time.ZonedDateTime)39 Duration (java.time.Duration)30 Clock (java.time.Clock)26 Lifetime (org.apache.cxf.sts.request.Lifetime)26 OffsetDateTime (java.time.OffsetDateTime)23 LocalDateTime (java.time.LocalDateTime)20 ArrayList (java.util.ArrayList)18 Element (org.w3c.dom.Element)18 LocalDate (java.time.LocalDate)17 IOException (java.io.IOException)14 Date (java.util.Date)14 LocalTime (java.time.LocalTime)12 DateTimeFormatter (java.time.format.DateTimeFormatter)12 STSException (org.apache.cxf.ws.security.sts.provider.STSException)12 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)12 Timestamp (java.sql.Timestamp)11 DefaultConditionsProvider (org.apache.cxf.sts.token.provider.DefaultConditionsProvider)10