Java Code Examples for java.sql.Timestamp#from()

The following examples show how to use java.sql.Timestamp#from() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LastUpdatedParmBehaviorUtilTest.java    From FHIR with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: EdmTimeOfDay.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@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.");
  }
}
 
Example 3
Source File: SqlStatementsSource.java    From ShedLock with Apache License 2.0 5 votes vote down vote up
@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;
    }
}
 
Example 4
Source File: TimestampTests.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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");
}
 
Example 5
Source File: SQLiteArtifactCache.java    From buck with Apache License 2.0 5 votes vote down vote up
/** 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);
}
 
Example 6
Source File: StatusChangeEvent.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: InstalledUpgradesTest.java    From Alpine with Apache License 2.0 5 votes vote down vote up
@Test
public void startedTest() {
    Timestamp ts = Timestamp.from(new Date().toInstant());
    InstalledUpgrades upgrades = new InstalledUpgrades();
    upgrades.setStartTime(ts);
    Assert.assertEquals(ts, upgrades.getStartTime());
}
 
Example 8
Source File: TimestampTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@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");
}
 
Example 9
Source File: HibPasswordRecovery.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * 
 */
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));
}
 
Example 10
Source File: TimestampTests.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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");
}
 
Example 11
Source File: TimestampTests.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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");
}
 
Example 12
Source File: JavatimeTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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!");
}
 
Example 13
Source File: InstantConverter.java    From runelite with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Object toDatabaseParam(Instant val)
{
	return Timestamp.from(val);
}
 
Example 14
Source File: InstantConverter.java    From runelite with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Object toDatabaseParam(Instant val)
{
	return Timestamp.from(val);
}
 
Example 15
Source File: InstantTimeJPAConverter.java    From patient-batch-loader with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Timestamp convertToDatabaseColumn(Instant instant) {
	return (instant == null ? null : Timestamp.from(instant));
}
 
Example 16
Source File: UpdateAutoTimestampConverter.java    From nomulus with Apache License 2.0 4 votes vote down vote up
@Override
public Timestamp convertToDatabaseColumn(UpdateAutoTimestamp entity) {
  return Timestamp.from(DateTimeUtils.toZonedDateTime(jpaTm().getTransactionTime()).toInstant());
}
 
Example 17
Source File: JavatimeTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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!");
}
 
Example 18
Source File: LocalTimeJavaDescriptor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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 );
}
 
Example 19
Source File: DateTimeHandler.java    From FHIR with Apache License 2.0 4 votes vote down vote up
public static Timestamp generateTimestamp(Instant inst) {
    return Timestamp.from(inst);
}
 
Example 20
Source File: MetricObservation.java    From chronix.spark with Apache License 2.0 2 votes vote down vote up
/**
 * 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));
}