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

The following examples show how to use java.sql.Timestamp#equals() . 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: AsOfAttribute.java    From reladomo with Apache License 2.0 6 votes vote down vote up
@Override
public Operation in(final List objects, final Extractor extractor)
{
    TimestampExtractor timestampExtractor = (TimestampExtractor) extractor;
    Timestamp value = null;
    for (int i = 0, n = objects.size(); i < n; i++)
    {
        final Timestamp other = timestampExtractor.timestampValueOf(objects.get(i));
        if (value == null)
        {
            value = other;
        }
        else
        {
            if (other != null && !other.equals(value))
            {
                throw new RuntimeException("only one as of attribute value is supported");
            }
        }
    }
    if (value == null)
    {
        throw new RuntimeException("must have at least one non-null as of attribute value");
    }
    return this.eq(value);
}
 
Example 2
Source File: MithraDatedTransactionalObjectImpl.java    From reladomo with Apache License 2.0 6 votes vote down vote up
protected MithraDataObject zSetTimestamp(TimestampAttribute attr, Timestamp newValue, boolean isReadOnly, Timestamp exclusiveUntil)
{
    DatedTransactionalBehavior behavior = this.zGetTransactionalBehaviorForWrite();
    MithraDataObject data = behavior.getCurrentDataForRead(this);
    Timestamp cur = attr.timestampValueOf(data);
    if (zMustCheckCurrent(data, exclusiveUntil))
    {
        if (cur == null)
        {
            if (newValue == null) return null;
        }
        else
        {
            if (cur.equals(newValue)) return null;
        }
    }
    return behavior.updateUntil(this, attr, newValue, exclusiveUntil, isReadOnly, true);
}
 
Example 3
Source File: MithraDatedTransactionalObjectImpl.java    From reladomo with Apache License 2.0 6 votes vote down vote up
protected MithraDataObject zSetTimestamp(TimestampAttribute attr, Timestamp newValue, boolean isReadOnly)
{
    DatedTransactionalBehavior behavior = this.zGetTransactionalBehaviorForWrite();
    MithraDataObject data = behavior.getCurrentDataForRead(this);
    Timestamp cur = attr.timestampValueOf(data);
    if (zMustCheckCurrent(data))
    {
        if (cur == null)
        {
            if (newValue == null) return null;
        }
        else
        {
            if (cur.equals(newValue)) return null;
        }
    }
    return behavior.update(this, attr, newValue, isReadOnly, true);
}
 
Example 4
Source File: TimestampAttribute.java    From reladomo with Apache License 2.0 5 votes vote down vote up
public <O> boolean valueEquals(Owner first, O second, Extractor<O, Timestamp> secondExtractor)
{
    Timestamp firstValue = this.timestampValueOf(first);
    Object secondValue = secondExtractor.valueOf(second);
    if (firstValue == secondValue) return true; // takes care of both null

    return (firstValue != null) && firstValue.equals(secondValue);
}
 
Example 5
Source File: AsOfAttributeInfiniteNull.java    From reladomo with Apache License 2.0 5 votes vote down vote up
public int appendWhereClauseForRange(Timestamp start, Timestamp end, StringBuffer whereClause)
{
    int numParams = 1;
    if (!end.equals(NullDataTimestamp.getInstance()))
    {
        whereClause.append(this.getFromAttribute().getColumnName());
        // note: this would be <= if we wanted to stich
        whereClause.append(" < ? and ");
        numParams = 2;
    }
    whereClause.append("("+this.getToAttribute().getColumnName());
    // note: this would be >= if we wanted to stich
    whereClause.append(" > ? or "+this.getToAttribute().getColumnName()+" is null)");
    return numParams;
}
 
Example 6
Source File: GradebookImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public int compare(Object gradebook, Object otherGradebook) {
      Timestamp modDate1 = ((Gradebook) gradebook).getLastUpdated();
      Timestamp modDate2 = ((Gradebook) otherGradebook).getLastUpdated();
      
      if(modDate1.equals(modDate2)) {
      	return compareTitles((Gradebook) gradebook, (Gradebook)otherGradebook);  
      }
    
       return modDate2.compareTo(modDate1);
}
 
Example 7
Source File: GradebookImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public int compare(Object gradebook, Object otherGradebook) {
	Timestamp modDate1 = ((Gradebook) gradebook).getLastUpdated();
	Timestamp modDate2 = ((Gradebook) otherGradebook).getLastUpdated();
     
	if(modDate1.equals(modDate2)) {
		return compareTitles((Gradebook) gradebook, (Gradebook)otherGradebook);  
   		}
     
	return modDate1.compareTo(modDate2);
}
 
Example 8
Source File: MithraTransactionalObjectImpl.java    From reladomo with Apache License 2.0 5 votes vote down vote up
protected MithraDataObject zSetTimestamp(TimestampAttribute attr, Timestamp newValue, boolean isReadOnly, boolean hasOptimistic)
{
    TransactionalBehavior behavior = zGetTransactionalBehaviorForWriteWithWaitIfNecessary();
    try
    {
        MithraDataObject data = behavior.getCurrentDataForWrite(this);
        Timestamp cur = attr.timestampValueOf(data);
        if (cur == null)
        {
            if (newValue == null) return null;
        }
        else
        {
            if (cur.equals(newValue)) return null;
        }
        data = behavior.update(this, attr, newValue, isReadOnly, true);
        if (hasOptimistic)
        {
            zIncrementOptimiticAttribute(behavior, data);
        }
        return data;
    }
    finally
    {
        behavior.clearTempTransaction(this);
    }
}
 
Example 9
Source File: TimestampTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compares two Timestamps with the expected result.
 *
 * @param ts1 the first Timestamp
 * @param ts2 the second Timestamp
 * @param expect the expected relation between ts1 and ts2; 0 if
 * ts1 equals to ts2, or 1 if ts1 is after ts2, or -1 if ts1 is
 * before ts2.
 */
private void compareTimestamps(Timestamp ts1, Timestamp ts2, int expected) {
    boolean expectedResult = expected > 0;
    boolean result = ts1.after(ts2);
    if (result != expectedResult) {
        errln("ts1.after(ts2) returned " + result
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }

    expectedResult = expected < 0;
    result = ts1.before(ts2);
    if (result != expectedResult) {
        errln("ts1.before(ts2) returned " + result
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }

    expectedResult = expected == 0;
    result = ts1.equals(ts2);
    if (result != expectedResult) {
        errln("ts1.equals(ts2) returned " + result
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }

    int x = ts1.compareTo(ts2);
    int y = (x > 0) ? 1 : (x < 0) ? -1 : 0;
    if (y != expected) {
        errln("ts1.compareTo(ts2) returned " + x + ", expected "
              + relation(expected, "") + "0"
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }
    long t1 = ts1.getTime();
    long t2 = ts2.getTime();
    int z = (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0;
    if (z == 0) {
        int n1 = ts1.getNanos();
        int n2 = ts2.getNanos();
        z = (n1 > n2) ? 1 : (n1 < n2) ? -1 : 0;
    }
    if (z != expected) {
        errln("ts1.getTime() " + relation(z, "==") + " ts2.getTime(), expected "
              + relation(expected, "==")
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }
}
 
Example 10
Source File: JavatimeTest.java    From dragonwell8_jdk 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 11
Source File: JavatimeTest.java    From openjdk-8-source 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 12
Source File: TimeMachineGenerator.java    From dkpro-jwpl with Apache License 2.0 4 votes vote down vote up
@Override
public void start() throws Exception {

	Timestamp fromTimestamp = configuration.getFromTimestamp();
	Timestamp toTimestamp = configuration.getToTimestamp();
	Integer each = configuration.getEach();

	Integer snapshotsCount = fromTimestamp.equals(toTimestamp) ? 1
			: calculateSnapshotsCount(fromTimestamp, toTimestamp, each);

	if (snapshotsCount > 0) {

		versions = new IDumpVersion[snapshotsCount];
		logger.log("Dumps to be generated:");

		for (int i = 0; i < snapshotsCount; i++) {

			Timestamp currentTimestamp = TimestampUtil.getNextTimestamp(
					fromTimestamp, i * each);
			logger.log(currentTimestamp);

			MetaData commonMetaData = MetaData
					.initWithConfig(configuration);
			commonMetaData.setTimestamp(currentTimestamp);

			IDumpVersion version = environmentFactory.getDumpVersion();

			version.initialize(currentTimestamp);
			version.setMetaData(commonMetaData);
			TimeMachineFiles currentFiles = new TimeMachineFiles(
					initialFiles);
			currentFiles.setTimestamp(currentTimestamp);
			version.setFiles(currentFiles);
			versions[i] = version;
		}

		processInputDumps();

	} else {
		logger.log("No timestamps.");
	}
}
 
Example 13
Source File: JavatimeTest.java    From jdk8u-jdk 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 14
Source File: JavatimeTest.java    From openjdk-jdk8u-backup 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 15
Source File: JavatimeTest.java    From jdk8u-dev-jdk 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 16
Source File: ParaLikeBiTemporalDirector.java    From reladomo-scala with Apache License 2.0 4 votes vote down vote up
protected boolean inactivateOnSameDayUpdate(Timestamp asOfDate)
{
    return !asOfDate.equals(this.getCurrentBusinessDate());
}
 
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: TimestampTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compares two Timestamps with the expected result.
 *
 * @param ts1 the first Timestamp
 * @param ts2 the second Timestamp
 * @param expect the expected relation between ts1 and ts2; 0 if
 * ts1 equals to ts2, or 1 if ts1 is after ts2, or -1 if ts1 is
 * before ts2.
 */
private void compareTimestamps(Timestamp ts1, Timestamp ts2, int expected) {
    boolean expectedResult = expected > 0;
    boolean result = ts1.after(ts2);
    if (result != expectedResult) {
        errln("ts1.after(ts2) returned " + result
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }

    expectedResult = expected < 0;
    result = ts1.before(ts2);
    if (result != expectedResult) {
        errln("ts1.before(ts2) returned " + result
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }

    expectedResult = expected == 0;
    result = ts1.equals(ts2);
    if (result != expectedResult) {
        errln("ts1.equals(ts2) returned " + result
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }

    int x = ts1.compareTo(ts2);
    int y = (x > 0) ? 1 : (x < 0) ? -1 : 0;
    if (y != expected) {
        errln("ts1.compareTo(ts2) returned " + x + ", expected "
              + relation(expected, "") + "0"
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }
    long t1 = ts1.getTime();
    long t2 = ts2.getTime();
    int z = (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0;
    if (z == 0) {
        int n1 = ts1.getNanos();
        int n2 = ts2.getNanos();
        z = (n1 > n2) ? 1 : (n1 < n2) ? -1 : 0;
    }
    if (z != expected) {
        errln("ts1.getTime() " + relation(z, "==") + " ts2.getTime(), expected "
              + relation(expected, "==")
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }
}
 
Example 19
Source File: TimestampTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Compares two Timestamps with the expected result.
 *
 * @param ts1 the first Timestamp
 * @param ts2 the second Timestamp
 * @param expect the expected relation between ts1 and ts2; 0 if
 * ts1 equals to ts2, or 1 if ts1 is after ts2, or -1 if ts1 is
 * before ts2.
 */
private void compareTimestamps(Timestamp ts1, Timestamp ts2, int expected) {
    boolean expectedResult = expected > 0;
    boolean result = ts1.after(ts2);
    if (result != expectedResult) {
        errln("ts1.after(ts2) returned " + result
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }

    expectedResult = expected < 0;
    result = ts1.before(ts2);
    if (result != expectedResult) {
        errln("ts1.before(ts2) returned " + result
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }

    expectedResult = expected == 0;
    result = ts1.equals(ts2);
    if (result != expectedResult) {
        errln("ts1.equals(ts2) returned " + result
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }

    int x = ts1.compareTo(ts2);
    int y = (x > 0) ? 1 : (x < 0) ? -1 : 0;
    if (y != expected) {
        errln("ts1.compareTo(ts2) returned " + x + ", expected "
              + relation(expected, "") + "0"
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }
    long t1 = ts1.getTime();
    long t2 = ts2.getTime();
    int z = (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0;
    if (z == 0) {
        int n1 = ts1.getNanos();
        int n2 = ts2.getNanos();
        z = (n1 > n2) ? 1 : (n1 < n2) ? -1 : 0;
    }
    if (z != expected) {
        errln("ts1.getTime() " + relation(z, "==") + " ts2.getTime(), expected "
              + relation(expected, "==")
              + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
    }
}
 
Example 20
Source File: JavatimeTest.java    From TencentKona-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!");
}