@Override public java.sql.Time convertToPersisted(LocalTime value) { if (value == null) { return null; } return java.sql.Time.valueOf(value); }
@Override public Time toDatabaseType(LocalTime value) { return Time.valueOf(value); }
/** * 将字符串类型的时间转换为 Time * * @param time * @return */ public static Time timeParse(String time) { return Time.valueOf(time); }
@Override public java.sql.Time convert(String s) throws ParseException { int len = s.length(); if (len == timeWithoutSecPatternLen) { s = s + ":00"; } if (len > timePatternLen) { s = s.substring(0, timePatternLen); } return java.sql.Time.valueOf(s); } }
public TimeValue(String value) { this.value = Time.valueOf(value.substring(1, value.length() - 1)); }
@Override public Time convertToDatabaseColumn(LocalTime localTime) { return localTime == null ? null : Time.valueOf( localTime ); }
public static Time castToTime(Object x, int targetSqlType) throws SQLException { if (x instanceof Time) { return (Time) x; } if (x instanceof java.util.Date) { return new Time(((java.util.Date) x).getTime()); } if (x instanceof LocalTime) { return Time.valueOf((LocalTime) x); } if (x instanceof LocalDateTime) { return Time.valueOf(((LocalDateTime) x).toLocalTime()); } try { if (x instanceof String) { return Time.valueOf((String) x); } } catch (RuntimeException e) { throw invalidConversion(x, targetSqlType, e); } throw invalidConversion(x, targetSqlType); }
/** * Static utility to parse a field of type Time from a byte sequence that represents text * characters * (such as when read from a file stream). * * @param bytes The bytes containing the text data that should be parsed. * @param startPos The offset to start the parsing. * @param length The length of the byte sequence (counting from the offset). * @param delimiter The delimiter that terminates the field. * @return The parsed value. * @throws IllegalArgumentException Thrown when the value cannot be parsed because the text * represents not a correct number. */ public static final Time parseField(byte[] bytes, int startPos, int length, char delimiter) { final int limitedLen = nextStringLength(bytes, startPos, length, delimiter); if (limitedLen > 0 && (Character.isWhitespace(bytes[startPos]) || Character.isWhitespace(bytes[startPos + limitedLen - 1]))) { throw new NumberFormatException("There is leading or trailing whitespace in the numeric field."); } final String str = new String(bytes, startPos, limitedLen, ConfigConstants.DEFAULT_CHARSET); return Time.valueOf(str); } }
@Override public int parseField(byte[] bytes, int startPos, int limit, byte[] delimiter, Time reusable) { final int endPos = nextStringEndPos(bytes, startPos, limit, delimiter); if (endPos < 0) { return -1; } String str = new String(bytes, startPos, endPos - startPos, ConfigConstants.DEFAULT_CHARSET); try { this.result = Time.valueOf(str); return (endPos == limit) ? limit : endPos + delimiter.length; } catch (IllegalArgumentException e) { setErrorState(ParseErrorState.NUMERIC_VALUE_FORMAT_ERROR); return -1; } }
private static Time toTime(int year, int month, int day, int hour, int minute, int second) { return Time.valueOf(LocalDateTime.of(year, month, day, hour, minute, second).toLocalTime()); } }
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()))); } }
@Override protected Object convert(Object source, Class<?> targetType) { assertEquals(Time.class, targetType, "Can only convert to " + Time.class.getCanonicalName()); try { return Time.valueOf(source.toString()); } catch (Exception e) { fail("failure while converting " + source + " into an instance of " + targetType.getCanonicalName()); } return null; }
@Test public void testConvertTimestamp() throws SQLException { LocalDateTime dateTime = LocalDateTime.of(2001, 5, 6, 12, 34, 56); Date sqlDate = Date.valueOf(dateTime.toLocalDate()); Time sqlTime = Time.valueOf(dateTime.toLocalTime()); Timestamp sqlTimestamp = Timestamp.valueOf(dateTime); java.util.Date javaDate = java.util.Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant()); assertParameter(sqlTimestamp, Types.TIMESTAMP, (ps, i) -> ps.setTimestamp(i, sqlTimestamp)); assertParameter(sqlTimestamp, Types.TIMESTAMP, (ps, i) -> ps.setObject(i, sqlTimestamp)); assertParameter(new Timestamp(sqlDate.getTime()), Types.TIMESTAMP, (ps, i) -> ps.setObject(i, sqlDate, Types.TIMESTAMP)); assertParameter(new Timestamp(sqlTime.getTime()), Types.TIMESTAMP, (ps, i) -> ps.setObject(i, sqlTime, Types.TIMESTAMP)); assertParameter(sqlTimestamp, Types.TIMESTAMP, (ps, i) -> ps.setObject(i, sqlTimestamp, Types.TIMESTAMP)); assertParameter(sqlTimestamp, Types.TIMESTAMP, (ps, i) -> ps.setObject(i, javaDate, Types.TIMESTAMP)); assertParameter(sqlTimestamp, Types.TIMESTAMP, (ps, i) -> ps.setObject(i, dateTime, Types.TIMESTAMP)); assertParameter(sqlTimestamp, Types.TIMESTAMP, (ps, i) -> ps.setObject(i, "2001-05-06 12:34:56", Types.TIMESTAMP)); }
@Test public void testConvertTime() throws SQLException { LocalTime time = LocalTime.of(12, 34, 56); Time sqlTime = Time.valueOf(time); java.util.Date javaDate = new java.util.Date(sqlTime.getTime()); LocalDateTime dateTime = LocalDateTime.of(LocalDate.of(2001, 5, 6), time); Timestamp sqlTimestamp = Timestamp.valueOf(dateTime); assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setTime(i, sqlTime)); assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setObject(i, sqlTime)); assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setObject(i, sqlTime, Types.TIME)); assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setObject(i, sqlTimestamp, Types.TIME)); assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setObject(i, javaDate, Types.TIME)); assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setObject(i, dateTime, Types.TIME)); assertParameter(sqlTime, Types.TIME, (ps, i) -> ps.setObject(i, "12:34:56", Types.TIME)); }
/** */ @Test public void testPassTableWithTimeKeyCreation() { final String creationQry = "CREATE TABLE %s (id TIME primary key, dateField TIME) " + "WITH \"cache_name=%s, WRAP_VALUE=false\""; Map<Time, Time> ent = new HashMap<>(); ent.put(Time.valueOf(LocalTime.now()), Time.valueOf(LocalTime.now().minusHours(1))); ent.put(Time.valueOf(LocalTime.now().minusHours(2)), Time.valueOf(LocalTime.now().minusHours(3))); checkInsertUpdateDelete(creationQry, "Tab2", ent); }
@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 } }