java.sql.Time.valueOf()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(228)

本文整理了Java中java.sql.Time.valueOf()方法的一些代码示例,展示了Time.valueOf()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Time.valueOf()方法的具体详情如下:
包路径:java.sql.Time
类名称:Time
方法名:valueOf

Time.valueOf介绍

[英]Creates a Time object from a string holding a time represented in JDBC escape format: hh:mm:ss.

An exception occurs if the input string does not comply with this format.
[中]从一个字符串创建一个时间对象,该字符串包含以JDBC转义格式表示的时间:hh:mm:ss。
如果输入字符串不符合此格式,则会发生异常。

代码示例

代码示例来源:origin: requery/requery

@Override
public java.sql.Time convertToPersisted(LocalTime value) {
  if (value == null) {
    return null;
  }
  return java.sql.Time.valueOf(value);
}

代码示例来源:origin: speedment/speedment

@Override
public Time toDatabaseType(LocalTime value) {
  return Time.valueOf(value);
}

代码示例来源:origin: cachecats/coderiver

/**
 * 将字符串类型的时间转换为 Time
 *
 * @param time
 * @return
 */
public static Time timeParse(String time) {
  return Time.valueOf(time);
}

代码示例来源:origin: jfinal/jfinal

@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);
  }
}

代码示例来源:origin: apache/flink

@Override
public Time[] getValidTestResults() {
  return new Time[] {
    Time.valueOf("00:00:00"), Time.valueOf("02:42:25"), Time.valueOf("14:15:51"),
    Time.valueOf("18:00:45"), Time.valueOf("23:59:58"), Time.valueOf("0:0:0")
  };
}

代码示例来源:origin: apache/flink

@Override
  protected Time[] getSortedTestData() {
    return new Time[] {
      Time.valueOf("00:00:00"),
      Time.valueOf("02:42:25"),
      Time.valueOf("14:15:59"),
      Time.valueOf("18:00:45")
    };
  }
}

代码示例来源:origin: JSQLParser/JSqlParser

public TimeValue(String value) {
  this.value = Time.valueOf(value.substring(1, value.length() - 1));
}

代码示例来源:origin: apache/flink

@Override
  protected Time[] getTestData() {
    return new Time[] {
      new Time(0L),
      Time.valueOf("00:00:00"),
      Time.valueOf("02:42:25"),
      Time.valueOf("14:15:59"),
      Time.valueOf("18:00:45")
    };
  }
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
public Time convertToDatabaseColumn(LocalTime localTime) {
  return localTime == null ? null : Time.valueOf( localTime );
}

代码示例来源:origin: prestodb/presto

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);
}

代码示例来源:origin: apache/flink

/**
   * 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);
  }
}

代码示例来源:origin: apache/flink

@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;
  }
}

代码示例来源:origin: prestodb/presto

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());
  }
}

代码示例来源:origin: jdbi/jdbi

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())));
  }
}

代码示例来源:origin: oblac/jodd

@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;
}

代码示例来源:origin: prestodb/presto

@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));
}

代码示例来源:origin: apache/ignite

/** */
@Test
public void testTime() throws Exception {
  testPutGet(ValueTime.get(Time.valueOf("10:01:01")),
    ValueTime.get(Time.valueOf("11:02:02")),
    ValueTime.get(Time.valueOf("12:03:03")));
}

代码示例来源:origin: prestodb/presto

@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));
}

代码示例来源:origin: apache/ignite

/** */
@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);
}

代码示例来源:origin: prestodb/presto

@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
  }
}

相关文章