@Override public Object toDatabaseParam(Instant val) { return Timestamp.from(val); }
@Override public Timestamp convertToPersisted(ZonedDateTime value) { if (value == null) { return null; } Instant instant = value.toInstant(); return Timestamp.from(instant); }
@Override public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException { if (instant != null) { if (calendar.isPresent()) { // We need to make a clone, because Calendar is not thread-safe // and some JDBC drivers mutate it during time calculations final Calendar calendarClone = (Calendar) calendar.get().clone(); statement.setTimestamp(position, Timestamp.from(instant), calendarClone); } else { statement.setTimestamp(position, Timestamp.from(instant)); } } else { statement.setNull(position, Types.TIMESTAMP); } } }
@Override public void apply(final int position, final PreparedStatement statement, final StatementContext ctx) throws SQLException { if (value != null) { if (calendar.isPresent()) { // We need to make a clone, because Calendar is not thread-safe // and some JDBC drivers mutate it during time calculations final Calendar calendarClone = (Calendar) calendar.get().clone(); statement.setTimestamp(position, Timestamp.from(value.toInstant()), calendarClone); } else { statement.setTimestamp(position, Timestamp.from(value.toInstant())); } } else { statement.setNull(position, Types.TIMESTAMP); } } }
ps.setTimestamp(2, new java.sql.Timestamp(System.currentTimeMillis())); // Since Java 8 ps.setTimestamp(2, java.sql.Timestamp.from(java.time.Instant.now())); ps.setTimestamp(2, java.sql.Timestamp.valueOf(java.time.LocalDateTime.now()));
@Override public Timestamp convertToPersisted(OffsetDateTime value) { if (value == null) { return null; } Instant instant = value.toInstant(); return Timestamp.from(instant); }
@Override public void apply(final int position, final PreparedStatement statement, final StatementContext ctx) throws SQLException { if (value != null) { if (calendar.isPresent()) { // We need to make a clone, because Calendar is not thread-safe // and some JDBC drivers mutate it during time calculations final Calendar calendarClone = (Calendar) calendar.get().clone(); statement.setTimestamp(position, Timestamp.from(value.toInstant()), calendarClone); } else { statement.setTimestamp(position, Timestamp.from(value.toInstant())); } } else { statement.setNull(position, Types.TIMESTAMP); } } }
private Timestamp convertTimeStamp(String timeString) { /* From RFC 5424: https://tools.ietf.org/html/rfc5424#page-11 The TIMESTAMP field is a formalized timestamp derived from [RFC3339]. Whereas [RFC3339] makes allowances for multiple syntaxes, this document imposes further restrictions. The TIMESTAMP value MUST follow these restrictions: o The "T" and "Z" characters in this syntax MUST be upper case. o Usage of the "T" character is REQUIRED. o Leap seconds MUST NOT be used. */ if (timeString == null) { return null; } return Timestamp.from(Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse(timeString))); } }
JavaTimeArgumentFactory() { register(Instant.class, Types.TIMESTAMP, (p, i, v) -> p.setTimestamp(i, Timestamp.from(v))); register(LocalDate.class, Types.DATE, (p, i, v) -> p.setDate(i, java.sql.Date.valueOf(v))); register(LocalTime.class, Types.TIME, (p, i, v) -> p.setTime(i, Time.valueOf(v))); register(LocalDateTime.class, Types.TIMESTAMP, (p, i, v) -> p.setTimestamp(i, Timestamp.valueOf(v))); register(OffsetDateTime.class, Types.TIMESTAMP, (p, i, v) -> p.setTimestamp(i, Timestamp.from(v.toInstant()))); register(ZonedDateTime.class, Types.TIMESTAMP, (p, i, v) -> p.setTimestamp(i, Timestamp.from(v.toInstant()))); } }
/** * Converts a string object for an object type of {@link Timestamp}. * If the column definition allows null and default value is 0000-00-00 00:00:00, we need return null, * else 0000-00-00 00:00:00 will be replaced with 1970-01-01 00:00:00; * * @param column the column definition describing the {@code data} value; never null * @param value the string object to be converted into a {@link Timestamp} type; * @return the converted value; */ private Object convertToTimestamp(Column column, String value) { final boolean matches = EPOCH_EQUIVALENT_TIMESTAMP.matcher(value).matches() || "0".equals(value) || EPOCH_TIMESTAMP.equals(value); if (matches) { if (column.isOptional()) { return null; } return Timestamp.from(Instant.EPOCH); } return Timestamp.valueOf(value).toInstant().atZone(ZoneId.systemDefault()); }
@Override @SuppressWarnings("unchecked") public <X> X unwrap(ZonedDateTime zonedDateTime, Class<X> type, WrapperOptions options) { if ( zonedDateTime == null ) { return null; } if ( ZonedDateTime.class.isAssignableFrom( type ) ) { return (X) zonedDateTime; } if ( Calendar.class.isAssignableFrom( type ) ) { return (X) GregorianCalendar.from( zonedDateTime ); } if ( Timestamp.class.isAssignableFrom( type ) ) { return (X) Timestamp.from( zonedDateTime.toInstant() ); } if ( java.sql.Date.class.isAssignableFrom( type ) ) { return (X) java.sql.Date.from( zonedDateTime.toInstant() ); } if ( java.sql.Time.class.isAssignableFrom( type ) ) { return (X) java.sql.Time.from( zonedDateTime.toInstant() ); } if ( Date.class.isAssignableFrom( type ) ) { return (X) Date.from( zonedDateTime.toInstant() ); } if ( Long.class.isAssignableFrom( type ) ) { return (X) Long.valueOf( zonedDateTime.toInstant().toEpochMilli() ); } throw unknownUnwrap( type ); }
return (X) Timestamp.from( instant );
return (X) java.sql.Timestamp.from( instant );
@Override @SuppressWarnings("unchecked") public <X> X unwrap(Instant instant, Class<X> type, WrapperOptions options) { if ( instant == null ) { return null; } if ( Instant.class.isAssignableFrom( type ) ) { return (X) instant; } if ( Calendar.class.isAssignableFrom( type ) ) { final ZoneId zoneId = ZoneId.ofOffset( "UTC", ZoneOffset.UTC ); return (X) GregorianCalendar.from( instant.atZone( zoneId ) ); } if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) { return (X) Timestamp.from( instant ); } if ( java.sql.Date.class.isAssignableFrom( type ) ) { return (X) java.sql.Date.from( instant ); } if ( java.sql.Time.class.isAssignableFrom( type ) ) { return (X) java.sql.Time.from( instant ); } if ( java.util.Date.class.isAssignableFrom( type ) ) { return (X) Date.from( instant ); } if ( Long.class.isAssignableFrom( type ) ) { return (X) Long.valueOf( instant.toEpochMilli() ); } throw unknownUnwrap( type ); }
private Timestamp now() { return Timestamp.from( LocalDateTime.now().toInstant( ZoneOffset.UTC ) ); } }
@Override @SuppressWarnings("unchecked") public <X> X unwrap(OffsetDateTime offsetDateTime, Class<X> type, WrapperOptions options) { if ( offsetDateTime == null ) { return null; } if ( OffsetDateTime.class.isAssignableFrom( type ) ) { return (X) offsetDateTime; } if ( Calendar.class.isAssignableFrom( type ) ) { return (X) GregorianCalendar.from( offsetDateTime.toZonedDateTime() ); } if ( Timestamp.class.isAssignableFrom( type ) ) { return (X) Timestamp.from( offsetDateTime.toInstant() ); } if ( java.sql.Date.class.isAssignableFrom( type ) ) { return (X) java.sql.Date.from( offsetDateTime.toInstant() ); } if ( java.sql.Time.class.isAssignableFrom( type ) ) { return (X) java.sql.Time.from( offsetDateTime.toInstant() ); } if ( Date.class.isAssignableFrom( type ) ) { return (X) Date.from( offsetDateTime.toInstant() ); } if ( Long.class.isAssignableFrom( type ) ) { return (X) Long.valueOf( offsetDateTime.toInstant().toEpochMilli() ); } throw unknownUnwrap( type ); }
@Override public void logBeforeExecution(StatementContext ctx) { String toString = ctx.getBinding() .findForName(name, ctx) .orElseThrow(AssertionError::new) .toString(); insertedTimestamp = OffsetDateTime.parse(toString); insertedSqlTimestamp = Timestamp.from(insertedTimestamp.toInstant()); }
@Test @RequiresDialect(H2Dialect.class) @RequiresDialect(PostgreSQL81Dialect.class) @RequiresDialect(MySQL5Dialect.class) public void test_hql_group_by_example_4() { doInJPA( this::entityManagerFactory, entityManager -> { Call call11 = new Call(); call11.setDuration( 10 ); call11.setTimestamp( Timestamp.from( LocalDateTime.of( 2000, 1, 1, 0, 0, 0 ).toInstant( ZoneOffset.UTC ) ) ); Phone phone = entityManager.createQuery( "select p from Phone p where p.calls is empty ", Phone.class).getResultList().get( 0 ); phone.addCall(call11); entityManager.flush(); entityManager.clear(); List<Object[]> personTotalCallDurations = entityManager.createQuery( "select p, sum( c.duration ) " + "from Call c " + "join c.phone p " + "group by p", Object[].class ) .getResultList(); assertEquals(2, personTotalCallDurations.size()); }); }
person1.setNickName( "JD" ); person1.setAddress( "Earth" ); person1.setCreatedOn( Timestamp.from( LocalDateTime.of( 2000, 1, 1, 0, 0, 0 ).toInstant( ZoneOffset.UTC ) )) ; person1.getAddresses().put( AddressType.HOME, "Home address" ); person1.getAddresses().put( AddressType.OFFICE, "Office address" ); person2.setCreatedOn( Timestamp.from( LocalDateTime.of( 2000, 1, 2, 12, 0, 0 ).toInstant( ZoneOffset.UTC ) )) ; entityManager.persist(person2); phone1.setType( PhoneType.MOBILE ); person1.addPhone( phone1 ); phone1.getRepairTimestamps().add( Timestamp.from( LocalDateTime.of( 2005, 1, 1, 12, 0, 0 ).toInstant( ZoneOffset.UTC ) ) ); phone1.getRepairTimestamps().add( Timestamp.from( LocalDateTime.of( 2006, 1, 1, 12, 0, 0 ).toInstant( ZoneOffset.UTC ) ) ); call11.setTimestamp( Timestamp.from( LocalDateTime.of( 2000, 1, 1, 0, 0, 0 ).toInstant( ZoneOffset.UTC ) ) ); call12.setTimestamp( Timestamp.from( LocalDateTime.of( 2000, 1, 1, 1, 0, 0 ).toInstant( ZoneOffset.UTC ) ) );
person1.setNickName( "JD" ); person1.setAddress( "Earth" ); person1.setCreatedOn( Timestamp.from( LocalDateTime.of( 2000, 1, 1, 0, 0, 0 ).toInstant( ZoneOffset.UTC ) ) );