java.sql.Timestamp#from ( )源码实例Demo

下面列出了java.sql.Timestamp#from ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: FHIR   文件: LastUpdatedParmBehaviorUtilTest.java
@SuppressWarnings("unused")
@Test
public void testPrecisionWithNotEqual() throws Exception {
    String vTime = "2019-12-11T00:00:00+00:00";
    TemporalAccessor v = DateTimeHandler.parse(vTime);
    Timestamp value = Timestamp.from(DateTimeHandler.generateValue(v, vTime));
    Timestamp lower = Timestamp.from(DateTimeHandler.generateLowerBound(Prefix.EQ, v, vTime));
    Timestamp upper = Timestamp.from(DateTimeHandler.generateUpperBound(Prefix.EQ, v, vTime));

    QueryParameter queryParm = generateQueryParameter(SearchConstants.Prefix.NE, null, vTime);
    List<Timestamp> expectedBindVariables = new ArrayList<>();
    expectedBindVariables.add(upper);
    expectedBindVariables.add(upper);
    expectedBindVariables.add(value);
    expectedBindVariables.add(value);

    String expectedSql =
            "((LAST_UPDATED < ? AND LAST_UPDATED > ?))";
    runTest(queryParm,
            expectedBindVariables,
            expectedSql);
}
 
源代码2 项目: ShedLock   文件: SqlStatementsSource.java
@NonNull
private Object timestamp(Instant time) {
    TimeZone timeZone = configuration.getTimeZone();
    if (timeZone == null) {
        return Timestamp.from(time);
    } else {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(Date.from(time));
        calendar.setTimeZone(timeZone);
        return calendar;
    }
}
 
源代码3 项目: olingo-odata4   文件: EdmTimeOfDay.java
@SuppressWarnings("unchecked")
@Override
protected <T> T internalValueOfString(final String value, final Boolean isNullable, final Integer maxLength,
    final Integer precision, final Integer scale, final Boolean isUnicode, final Class<T> returnType)
    throws EdmPrimitiveTypeException {
  LocalTime time;
  try {
    time = LocalTime.parse(value);
  } catch (DateTimeParseException ex) {
    throw new EdmPrimitiveTypeException("The literal '" + value + "' has illegal content.");
  }

  // appropriate types
  if (returnType.isAssignableFrom(LocalTime.class)) {
    return (T) time;
  } else if (returnType.isAssignableFrom(java.sql.Time.class)) {
    return (T) java.sql.Time.valueOf(time);
  }

  // inappropriate types, which need to be supported for backward compatibility
  ZonedDateTime zdt = LocalDateTime.of(EPOCH, time).atZone(ZoneId.systemDefault());
  if (returnType.isAssignableFrom(Calendar.class)) {
    return (T) GregorianCalendar.from(zdt);
  } else if (returnType.isAssignableFrom(Long.class)) {
    return (T) Long.valueOf(zdt.toInstant().toEpochMilli());
  } else if (returnType.isAssignableFrom(java.sql.Date.class)) {
    throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.");
  } else if (returnType.isAssignableFrom(Timestamp.class)) {
    return (T) Timestamp.from(zdt.toInstant());
  } else if (returnType.isAssignableFrom(java.util.Date.class)) {
    return (T) java.util.Date.from(zdt.toInstant());
  } else {
    throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.");
  }
}
 
源代码4 项目: openjdk-jdk9   文件: TimestampTests.java
@Test
public void test45() throws Exception {
    Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
    Instant instant = ts1.toInstant();
    Timestamp ts2 = Timestamp.from(instant);
    assertTrue(ts1.equals(ts2), "Error ts1 != ts2");
}
 
源代码5 项目: api-layer   文件: StatusChangeEvent.java
/**
 * Create a String time stamp for this event
 * @return a timestamp
 */
default String setTimeStamp() {
    Instant now = Instant.now();
    Timestamp current = Timestamp.from(now);
    DateFormat df = DateFormat.getDateTimeInstance();
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    return df.format(current);
}
 
源代码6 项目: cosmo   文件: HibPasswordRecovery.java
/**
 * 
 */
public HibPasswordRecovery(User user, String key, long timeout) {
    this.user = user;
    this.key = key;
    this.timeout = timeout;
    this.created = Timestamp.from(Instant.now().truncatedTo(ChronoUnit.SECONDS));
}
 
源代码7 项目: dragonwell8_jdk   文件: TimestampTests.java
@Test
public void test45() throws Exception {
    Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
    Instant instant = ts1.toInstant();
    Timestamp ts2 = Timestamp.from(instant);
    assertTrue(ts1.equals(ts2), "Error ts1 != ts2");
}
 
源代码8 项目: dragonwell8_jdk   文件: TimestampTests.java
@Test
public void test46() throws Exception {
    Instant instant = Instant.now();
    Timestamp ts2 = Timestamp.from(instant);
    assertTrue(instant.equals(ts2.toInstant()),
            "Error Instant values do not match");
}
 
源代码9 项目: jdk8u60   文件: TimestampTests.java
@Test
public void test45() throws Exception {
    Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
    Instant instant = ts1.toInstant();
    Timestamp ts2 = Timestamp.from(instant);
    assertTrue(ts1.equals(ts2), "Error ts1 != ts2");
}
 
源代码10 项目: Alpine   文件: InstalledUpgradesTest.java
@Test
public void startedTest() {
    Timestamp ts = Timestamp.from(new Date().toInstant());
    InstalledUpgrades upgrades = new InstalledUpgrades();
    upgrades.setStartTime(ts);
    Assert.assertEquals(ts, upgrades.getStartTime());
}
 
源代码11 项目: buck   文件: SQLiteArtifactCache.java
/** Removes metadata older than a computed eviction time. */
@VisibleForTesting
ListenableFuture<Unit> removeOldMetadata() {
  Timestamp evictionTime = Timestamp.from(Instant.now().minus(DEFAULT_EVICTION_TIME));
  try {
    int deleted = db.deleteMetadata(evictionTime);
    LOG.verbose("Removed %d metadata rows not accessed since %s", deleted, evictionTime);
  } catch (SQLException e) {
    LOG.error(e, "Failed to clean database");
  }

  return Futures.immediateFuture(null);
}
 
源代码12 项目: FHIR   文件: DateTimeHandler.java
public static Timestamp generateTimestamp(Instant inst) {
    return Timestamp.from(inst);
}
 
源代码13 项目: lams   文件: LocalTimeJavaDescriptor.java
@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 );
}
 
源代码14 项目: openjdk-8   文件: JavatimeTest.java
public static void main(String[] args) throws Throwable {
    int N = 10000;
    long t1970 = new java.util.Date(70, 0, 01).getTime();
    Random r = new Random();
    for (int i = 0; i < N; i++) {
        int days  = r.nextInt(50) * 365 + r.nextInt(365);
        long secs = t1970 + days * 86400 + r.nextInt(86400);
        int nanos = r.nextInt(NANOS_PER_SECOND);
        int nanos_ms = nanos / 1000000 * 1000000; // millis precision
        long millis = secs * 1000 + r.nextInt(1000);

        LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
        LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
        Instant inst = Instant.ofEpochSecond(secs, nanos);
        Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
        //System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);

        /////////// Timestamp ////////////////////////////////
        Timestamp ta = new Timestamp(millis);
        ta.setNanos(nanos);
        if (!isEqual(ta.toLocalDateTime(), ta)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ta.toLocalDateTime(), ta);
            throw new RuntimeException("FAILED: j.s.ts -> ldt");
        }
        if (!isEqual(ldt, Timestamp.valueOf(ldt))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ldt, Timestamp.valueOf(ldt));
            throw new RuntimeException("FAILED: ldt -> j.s.ts");
        }
        Instant inst0 = ta.toInstant();
        if (ta.getTime() != inst0.toEpochMilli() ||
            ta.getNanos() != inst0.getNano() ||
            !ta.equals(Timestamp.from(inst0))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts");
        }
        inst = Instant.ofEpochSecond(secs, nanos);
        Timestamp ta0 = Timestamp.from(inst);
        if (ta0.getTime() != inst.toEpochMilli() ||
            ta0.getNanos() != inst.getNano() ||
            !inst.equals(ta0.toInstant())) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            throw new RuntimeException("FAILED: instant -> timestamp -> instant");
        }

        ////////// java.sql.Date /////////////////////////////
        // j.s.d/t uses j.u.d.equals() !!!!!!!!
        java.sql.Date jsd = new java.sql.Date(millis);
        if (!isEqual(jsd.toLocalDate(), jsd)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(jsd.toLocalDate(), jsd);
            throw new RuntimeException("FAILED: j.s.d -> ld");
        }
        LocalDate ld = ldt.toLocalDate();
        if (!isEqual(ld, java.sql.Date.valueOf(ld))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ld, java.sql.Date.valueOf(ld));
            throw new RuntimeException("FAILED: ld -> j.s.d");
        }
        ////////// java.sql.Time /////////////////////////////
        java.sql.Time jst = new java.sql.Time(millis);
        if (!isEqual(jst.toLocalTime(), jst)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(jst.toLocalTime(), jst);
            throw new RuntimeException("FAILED: j.s.t -> lt");
        }
        // millis precision
        LocalTime lt = ldt_ms.toLocalTime();
        if (!isEqual(lt, java.sql.Time.valueOf(lt))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(lt, java.sql.Time.valueOf(lt));
            throw new RuntimeException("FAILED: lt -> j.s.t");
        }
    }
    System.out.println("Passed!");
}
 
源代码15 项目: nomulus   文件: UpdateAutoTimestampConverter.java
@Override
public Timestamp convertToDatabaseColumn(UpdateAutoTimestamp entity) {
  return Timestamp.from(DateTimeUtils.toZonedDateTime(jpaTm().getTransactionTime()).toInstant());
}
 
@Override
public Timestamp convertToDatabaseColumn(Instant instant) {
	return (instant == null ? null : Timestamp.from(instant));
}
 
源代码17 项目: runelite   文件: InstantConverter.java
@Override
public Object toDatabaseParam(Instant val)
{
	return Timestamp.from(val);
}
 
源代码18 项目: runelite   文件: InstantConverter.java
@Override
public Object toDatabaseParam(Instant val)
{
	return Timestamp.from(val);
}
 
源代码19 项目: hottub   文件: JavatimeTest.java
public static void main(String[] args) throws Throwable {
    int N = 10000;
    long t1970 = new java.util.Date(70, 0, 01).getTime();
    Random r = new Random();
    for (int i = 0; i < N; i++) {
        int days  = r.nextInt(50) * 365 + r.nextInt(365);
        long secs = t1970 + days * 86400 + r.nextInt(86400);
        int nanos = r.nextInt(NANOS_PER_SECOND);
        int nanos_ms = nanos / 1000000 * 1000000; // millis precision
        long millis = secs * 1000 + r.nextInt(1000);

        LocalDateTime ldt = LocalDateTime.ofEpochSecond(secs, nanos, ZoneOffset.UTC);
        LocalDateTime ldt_ms = LocalDateTime.ofEpochSecond(secs, nanos_ms, ZoneOffset.UTC);
        Instant inst = Instant.ofEpochSecond(secs, nanos);
        Instant inst_ms = Instant.ofEpochSecond(secs, nanos_ms);
        //System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);

        /////////// Timestamp ////////////////////////////////
        Timestamp ta = new Timestamp(millis);
        ta.setNanos(nanos);
        if (!isEqual(ta.toLocalDateTime(), ta)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ta.toLocalDateTime(), ta);
            throw new RuntimeException("FAILED: j.s.ts -> ldt");
        }
        if (!isEqual(ldt, Timestamp.valueOf(ldt))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ldt, Timestamp.valueOf(ldt));
            throw new RuntimeException("FAILED: ldt -> j.s.ts");
        }
        Instant inst0 = ta.toInstant();
        if (ta.getTime() != inst0.toEpochMilli() ||
            ta.getNanos() != inst0.getNano() ||
            !ta.equals(Timestamp.from(inst0))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            throw new RuntimeException("FAILED: j.s.ts -> instant -> j.s.ts");
        }
        inst = Instant.ofEpochSecond(secs, nanos);
        Timestamp ta0 = Timestamp.from(inst);
        if (ta0.getTime() != inst.toEpochMilli() ||
            ta0.getNanos() != inst.getNano() ||
            !inst.equals(ta0.toInstant())) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            throw new RuntimeException("FAILED: instant -> timestamp -> instant");
        }

        ////////// java.sql.Date /////////////////////////////
        // j.s.d/t uses j.u.d.equals() !!!!!!!!
        java.sql.Date jsd = new java.sql.Date(millis);
        if (!isEqual(jsd.toLocalDate(), jsd)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(jsd.toLocalDate(), jsd);
            throw new RuntimeException("FAILED: j.s.d -> ld");
        }
        LocalDate ld = ldt.toLocalDate();
        if (!isEqual(ld, java.sql.Date.valueOf(ld))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(ld, java.sql.Date.valueOf(ld));
            throw new RuntimeException("FAILED: ld -> j.s.d");
        }
        ////////// java.sql.Time /////////////////////////////
        java.sql.Time jst = new java.sql.Time(millis);
        if (!isEqual(jst.toLocalTime(), jst)) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(jst.toLocalTime(), jst);
            throw new RuntimeException("FAILED: j.s.t -> lt");
        }
        // millis precision
        LocalTime lt = ldt_ms.toLocalTime();
        if (!isEqual(lt, java.sql.Time.valueOf(lt))) {
            System.out.printf("ms: %16d  ns: %10d  ldt:[%s]%n", millis, nanos, ldt);
            print(lt, java.sql.Time.valueOf(lt));
            throw new RuntimeException("FAILED: lt -> j.s.t");
        }
    }
    System.out.println("Passed!");
}
 
源代码20 项目: chronix.spark   文件: MetricObservation.java
/**
 * Returns a Timestamp object which can be better
 * used in Spark SQL and/or Zeppelin.
 *
 * @return a Timestamp object
 */
public Timestamp getTime() {
    return Timestamp.from(Instant.ofEpochMilli(timestamp));
}