final ZonedDateTime zonedDateTime = value.atDate( LocalDate.of( 1970, 1, 1 ) ).atZone( ZoneId.systemDefault() );
/** * {@inheritDoc} * <p>Converts the {@code LocalTime} to {@link ZoneOffset#UTC} at EpochDay via {@link LocalTime#atDate(LocalDate)} and * {@link java.time.LocalDateTime#toInstant(ZoneOffset)}.</p> */ @Override public void encode(final BsonWriter writer, final LocalTime value, final EncoderContext encoderContext) { writer.writeDateTime(value.atDate(LocalDate.ofEpochDay(0L)).toInstant(ZoneOffset.UTC).toEpochMilli()); }
final DateTimeFormatter dtFormatter = getDateTimeFormatter(valueFormat); LocalTime parsedTime = LocalTime.parse(parameterValue, dtFormatter); LocalDateTime localDateTime = parsedTime.atDate(LocalDate.ofEpochDay(0)); Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant(); time = new Time(instant.toEpochMilli());
@Test public void testLocallyUnrepresentableTimeLiterals() { LocalDateTime localTimeThatDidNotExist = LocalDateTime.of(2017, 4, 2, 2, 10); checkState(ZoneId.systemDefault().getRules().getValidOffsets(localTimeThatDidNotExist).isEmpty(), "This test assumes certain JVM time zone"); // This tests that both Presto runner and H2 can return TIMESTAMP value that never happened in JVM's zone (e.g. is not representable using java.sql.Timestamp) @Language("SQL") String sql = DateTimeFormatter.ofPattern("'SELECT TIMESTAMP '''uuuu-MM-dd HH:mm:ss''").format(localTimeThatDidNotExist); assertEquals(computeScalar(sql), localTimeThatDidNotExist); // this tests Presto and the QueryRunner assertQuery(sql); // this tests H2QueryRunner LocalDate localDateThatDidNotHaveMidnight = LocalDate.of(1970, 1, 1); checkState(ZoneId.systemDefault().getRules().getValidOffsets(localDateThatDidNotHaveMidnight.atStartOfDay()).isEmpty(), "This test assumes certain JVM time zone"); // This tests that both Presto runner and H2 can return DATE value for a day which midnight never happened in JVM's zone (e.g. is not exactly representable using java.sql.Date) sql = DateTimeFormatter.ofPattern("'SELECT DATE '''uuuu-MM-dd''").format(localDateThatDidNotHaveMidnight); assertEquals(computeScalar(sql), localDateThatDidNotHaveMidnight); // this tests Presto and the QueryRunner assertQuery(sql); // this tests H2QueryRunner LocalTime localTimeThatDidNotOccurOn19700101 = LocalTime.of(0, 10); checkState(ZoneId.systemDefault().getRules().getValidOffsets(localTimeThatDidNotOccurOn19700101.atDate(LocalDate.ofEpochDay(0))).isEmpty(), "This test assumes certain JVM time zone"); checkState(!Objects.equals(java.sql.Time.valueOf(localTimeThatDidNotOccurOn19700101).toLocalTime(), localTimeThatDidNotOccurOn19700101), "This test assumes certain JVM time zone"); sql = DateTimeFormatter.ofPattern("'SELECT TIME '''HH:mm:ss''").format(localTimeThatDidNotOccurOn19700101); assertEquals(computeScalar(sql), localTimeThatDidNotOccurOn19700101); // this tests Presto and the QueryRunner assertQuery(sql); // this tests H2QueryRunner } }
LocalTime lt = ...; Instant instant = lt.atDate(LocalDate.of(A_YEAR, A_MONTH, A_DAY)). atZone(ZoneId.systemDefault()).toInstant(); Date time = Date.from(instant);
@Override public Date convert(LocalTime in, Context context) throws Exception { if (in == null) return null; return Date.from(in.atDate(LocalDate.now()).atZone(dateTimeZone).toInstant()); } }
@Override public Date convert(LocalTime in, Context context) throws Exception { if (in == null) return null; return Date.from(in.atDate(LocalDate.now()).atZone(dateTimeZone).toInstant()); } }
/** * @param localTime * @return a java.util.Date with time part filled from LocalTime (truncated to milliseconds). */ public static Date getDate(LocalTime localTime) { if (localTime != null) { return Date.from(localTime.atDate(LocalDate.ofEpochDay(0)).atZone(ZoneOffset.UTC).toInstant()); } return null; }
@Contract("null -> null; !null -> !null") public static @Nullable Date localTimeToUtilDate(final @Nullable LocalTime d) { if (d == null) { return null; } return Date.from(d.atDate(LocalDate.now()).atZone(ZoneId.systemDefault()).toInstant()); } }
/** * Translate a {@link TimeDelay} into a {@link LocalDateTime}. * * @param date the date to combien with, not null * @return a non-null {@link LocalDateTime} which is a combination of {@code expectedTime} * plus its {@code delay} and the {@code date}. */ public LocalDateTime atDate(LocalDate date) { return getEffectiveTime().atDate(date); }
@AtlasConversionInfo(sourceType = FieldType.TIME, targetType = FieldType.DATE_TIME) public LocalDateTime toLocalDateTime(LocalTime value) { return value != null ? value.atDate(LocalDate.now()) : null; }
@AtlasConversionInfo(sourceType = FieldType.TIME, targetType = FieldType.DATE_TIME_TZ) public ZonedDateTime toZonedDateTime(LocalTime value) { return value != null ? value.atDate(LocalDate.now()).atZone(ZoneId.systemDefault()) : null; }
@AtlasConversionInfo(sourceType = FieldType.TIME, targetType = FieldType.DATE_TIME) public java.sql.Timestamp toSqlTimestamp(LocalTime value) { return value != null ? java.sql.Timestamp.valueOf(value.atDate(LocalDate.now())) : null; }
private static Date durationToDate(final Duration duration) { if (duration.compareTo(MAXIMUM_DURATION) > 0) { throw new IllegalArgumentException("The duaration has to be smaller than 23:59:59."); } final LocalTime lt = LocalTime.ofNanoOfDay(duration.toNanos()); return Date.from(lt.atDate(LocalDate.now(ZONEID_UTC)).atZone(ZONEID_UTC).toInstant()); }
@AtlasConversionInfo(sourceType = FieldType.TIME, targetType = FieldType.DATE_TIME_TZ) public GregorianCalendar toGregorianCalendar(LocalTime value) { return value != null ? GregorianCalendar.from(value.atDate(LocalDate.now()).atZone(ZoneId.systemDefault())) : null; }
public void eval() { byte[] buf = new byte[in.end - in.start]; in.buffer.getBytes(in.start, buf, 0, in.end - in.start); String input = new String(buf, com.google.common.base.Charsets.UTF_8); java.time.format.DateTimeFormatter f = org.apache.drill.exec.expr.fn.impl.DateUtility.getTimeFormatter(); out.value = (int) (java.time.LocalTime.parse(input, f).atDate(java.time.LocalDate.ofEpochDay(0)).toInstant(java.time.ZoneOffset.UTC).toEpochMilli()); } }
public void eval() { if(in.end == in.start) { out.isSet = 0; return; } out.isSet = 1; byte[] buf = new byte[in.end - in.start]; in.buffer.getBytes(in.start, buf, 0, in.end - in.start); String input = new String(buf, com.google.common.base.Charsets.UTF_8); java.time.format.DateTimeFormatter f = org.apache.drill.exec.expr.fn.impl.DateUtility.getTimeFormatter(); out.value = (int) (java.time.LocalTime.parse(input, f).atDate(java.time.LocalDate.ofEpochDay(0)).toInstant(java.time.ZoneOffset.UTC).toEpochMilli()); } }
public void eval() { if(in.isSet == 0 || in.end == in.start) { out.isSet = 0; return; } out.isSet = 1; byte[] buf = new byte[in.end - in.start]; in.buffer.getBytes(in.start, buf, 0, in.end - in.start); String input = new String(buf, com.google.common.base.Charsets.UTF_8); java.time.format.DateTimeFormatter f = org.apache.drill.exec.expr.fn.impl.DateUtility.getTimeFormatter(); out.value = (int) (java.time.LocalTime.parse(input, f).atDate(java.time.LocalDate.ofEpochDay(0)).toInstant(java.time.ZoneOffset.UTC).toEpochMilli()); } }
@Test public void testJavaLocalTime() throws Exception { java.time.LocalTime value = java.time.LocalTime.now(); java.time.ZoneId zoneId = ZoneId.of("America/Los_Angeles"); newFieldMapperAndMapToPS(new ConstantGetter<Object, java.time.LocalTime>(value), java.time.LocalTime.class, new JavaZoneIdProperty(zoneId)); newFieldMapperAndMapToPS(NullGetter.<Object, java.time.LocalTime>getter(), java.time.LocalTime.class); verify(ps).setTime(1, new Time(value.atDate(LocalDate.now()).atZone(zoneId).toInstant().toEpochMilli())); verify(ps).setNull(2, Types.TIME); }