下面列出了java.sql.Timestamp#from ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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;
}
}
@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.");
}
}
@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");
}
/**
* 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);
}
/**
*
*/
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));
}
@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");
}
@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");
}
@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");
}
@Test
public void startedTest() {
Timestamp ts = Timestamp.from(new Date().toInstant());
InstalledUpgrades upgrades = new InstalledUpgrades();
upgrades.setStartTime(ts);
Assert.assertEquals(ts, upgrades.getStartTime());
}
/** 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);
}
public static Timestamp generateTimestamp(Instant inst) {
return Timestamp.from(inst);
}
@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 );
}
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!");
}
@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));
}
@Override
public Object toDatabaseParam(Instant val)
{
return Timestamp.from(val);
}
@Override
public Object toDatabaseParam(Instant val)
{
return Timestamp.from(val);
}
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!");
}
/**
* 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));
}