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

The following examples show how to use java.sql.Timestamp#setNanos() . 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: TimestampTests.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test41() throws Exception {
    int nanos = 0;
    Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
    ts1.setNanos(nanos);
    assertTrue(ts1.getNanos() == nanos, "Error Invalid Nanos value");
}
 
Example 2
Source File: TimestampTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test41() throws Exception {
    int nanos = 0;
    Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
    ts1.setNanos(nanos);
    assertTrue(ts1.getNanos() == nanos, "Error Invalid Nanos value");
}
 
Example 3
Source File: TimestampSerializer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Timestamp copy(Timestamp from) {
	if (from == null) {
		return null;
	}
	Timestamp t = new Timestamp(from.getTime());
	t.setNanos(from.getNanos());
	return t;
}
 
Example 4
Source File: MySQLTimeBinaryProtocolValue.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private Timestamp getTimestamp(final MySQLPacketPayload payload) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(0, Calendar.JANUARY, 0, payload.readInt1(), payload.readInt1(), payload.readInt1());
    Timestamp result = new Timestamp(calendar.getTimeInMillis());
    result.setNanos(0);
    return result;
}
 
Example 5
Source File: ConvertUtils.java    From aes-rsa-java with Apache License 2.0 5 votes vote down vote up
public static Timestamp getCurrentDate(){

        Calendar c = Calendar.getInstance();
        c.set(c.get(1), c.get(2), c.get(5), 0, 0, 0);
        Timestamp t = new Timestamp(c.getTime().getTime());
        t.setNanos(0);
        return t;
    }
 
Example 6
Source File: PrestoResultSet.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Timestamp parseTimestamp(String value, Function<String, ZoneId> timeZoneParser)
{
    Matcher matcher = DATETIME_PATTERN.matcher(value);
    if (!matcher.matches()) {
        throw new IllegalArgumentException("Invalid timestamp: " + value);
    }

    int year = Integer.parseInt(matcher.group("year"));
    int month = Integer.parseInt(matcher.group("month"));
    int day = Integer.parseInt(matcher.group("day"));
    int hour = Integer.parseInt(matcher.group("hour"));
    int minute = Integer.parseInt(matcher.group("minute"));
    int second = Integer.parseInt(matcher.group("second"));
    String fraction = matcher.group("fraction");
    ZoneId zoneId = timeZoneParser.apply(matcher.group("timezone"));

    long fractionValue = 0;
    int precision = 0;
    if (fraction != null) {
        precision = fraction.length();
        fractionValue = Long.parseLong(fraction);
    }

    long epochSecond = LocalDateTime.of(year, month, day, hour, minute, second, 0)
            .atZone(zoneId)
            .toEpochSecond();

    Timestamp timestamp = new Timestamp(epochSecond * 1000);
    timestamp.setNanos((int) rescale(fractionValue, precision, 9));
    return timestamp;
}
 
Example 7
Source File: TimestampTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test41() throws Exception {
    int nanos = 0;
    Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
    ts1.setNanos(nanos);
    assertTrue(ts1.getNanos() == nanos, "Error Invalid Nanos value");
}
 
Example 8
Source File: IndexMaintainerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompositeRowKeyTimeIndex() throws Exception {
    long timeInMillis = System.currentTimeMillis();
    long timeInNanos = System.nanoTime();
    Timestamp ts = new Timestamp(timeInMillis);
    ts.setNanos((int) (timeInNanos % 1000000000));
    testIndexRowKeyBuilding("ts1 DATE NOT NULL, ts2 TIME NOT NULL, ts3 TIMESTAMP NOT NULL", "ts1,ts2,ts3", "ts2, ts1", new Object [] {new Date(timeInMillis), new Time(timeInMillis), ts});
}
 
Example 9
Source File: java_sql_Timestamp.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
protected Timestamp getObject() {
    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    timestamp.setNanos(1 + timestamp.getNanos());
    return timestamp;
}
 
Example 10
Source File: EdmSimpleTypeTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Test
public void valueOfStringDateTime() throws Exception {
  final EdmSimpleType instance = EdmSimpleTypeKind.DateTime.getEdmSimpleTypeInstance();
  Calendar dateTime = Calendar.getInstance();

  dateTime.clear();
  dateTime.setTimeZone(TimeZone.getTimeZone("GMT"));
  dateTime.set(2012, 1, 29, 23, 32, 3);
  assertEquals(dateTime, instance.valueOfString("2012-02-29T23:32:03", EdmLiteralKind.DEFAULT, null, Calendar.class));
  assertEquals(Long.valueOf(dateTime.getTimeInMillis()), instance.valueOfString("2012-02-29T23:32:03",
      EdmLiteralKind.JSON, null, Long.class));
  assertEquals(dateTime, instance.valueOfString("/Date(1330558323000)/", EdmLiteralKind.JSON, null, Calendar.class));
  assertEquals(Long.valueOf(dateTime.getTimeInMillis()), instance.valueOfString("/Date(1330558323000)/",
      EdmLiteralKind.JSON, null, Long.class));
  assertEquals(dateTime.getTime(), instance.valueOfString("/Date(1330558323000)/", EdmLiteralKind.JSON, null,
      Date.class));
  assertEquals(dateTime.getTime(), instance.valueOfString("datetime'2012-02-29T23:32:03'", EdmLiteralKind.URI, null,
      Date.class));

  dateTime.add(Calendar.MILLISECOND, 1);
  assertEquals(Long.valueOf(dateTime.getTimeInMillis()), instance.valueOfString("2012-02-29T23:32:03.001",
      EdmLiteralKind.DEFAULT, null, Long.class));
  assertEquals(dateTime.getTime(), instance.valueOfString("/Date(1330558323001)/", EdmLiteralKind.JSON, null,
      Date.class));
  assertEquals(dateTime, instance.valueOfString("datetime'2012-02-29T23:32:03.001'", EdmLiteralKind.URI, null,
      Calendar.class));
  
  //OLINGO-883 prefix is case insensitive
  assertEquals(dateTime, instance.valueOfString("DaTeTiMe'2012-02-29T23:32:03.001'", EdmLiteralKind.URI, null,
      Calendar.class));

  dateTime.add(Calendar.MILLISECOND, 9);
  assertEquals(dateTime, instance.valueOfString("2012-02-29T23:32:03.01", EdmLiteralKind.DEFAULT,
      getPrecisionScaleFacets(2, null), Calendar.class));
  assertEquals(dateTime, instance.valueOfString("2012-02-29T23:32:03.0100000", EdmLiteralKind.DEFAULT,
      getPrecisionScaleFacets(2, null), Calendar.class));
  dateTime.add(Calendar.MILLISECOND, -10);
  assertEquals(dateTime, instance.valueOfString("2012-02-29T23:32:03.000", EdmLiteralKind.DEFAULT,
      getPrecisionScaleFacets(0, null), Calendar.class));
  dateTime.add(Calendar.MILLISECOND, -13);
  assertEquals(dateTime, instance.valueOfString("2012-02-29T23:32:02.987", EdmLiteralKind.DEFAULT,
      getPrecisionScaleFacets(null, null), Calendar.class));
  assertEquals(dateTime, instance.valueOfString("2012-02-29T23:32:02.98700", EdmLiteralKind.DEFAULT,
      getPrecisionScaleFacets(5, null), Calendar.class));
  dateTime.add(Calendar.MILLISECOND, 3);
  assertEquals(dateTime, instance.valueOfString("2012-02-29T23:32:02.99", EdmLiteralKind.DEFAULT,
      getPrecisionScaleFacets(2, null), Calendar.class));
  dateTime.add(Calendar.MILLISECOND, -90);
  assertEquals(dateTime, instance.valueOfString("2012-02-29T23:32:02.9", EdmLiteralKind.DEFAULT,
      getPrecisionScaleFacets(1, null), Calendar.class));
  dateTime.add(Calendar.MILLISECOND, -2900);
  assertEquals(dateTime, instance.valueOfString("2012-02-29T23:32", EdmLiteralKind.DEFAULT, null, Calendar.class));

  dateTime.clear();
  dateTime.setTimeZone(TimeZone.getTimeZone("GMT"));
  dateTime.set(1969, 11, 31, 23, 59, 18);
  assertEquals(dateTime, instance.valueOfString("/Date(-42000)/", EdmLiteralKind.JSON, null, Calendar.class));

  Timestamp timestamp = new Timestamp(0);
  timestamp.setNanos(987654321);
  assertEquals(timestamp, instance.valueOfString("1970-01-01T00:00:00.987654321", EdmLiteralKind.DEFAULT, null,
      Timestamp.class));

  expectErrorInValueOfString(instance, "2012-02-29T23:32:02.9", EdmLiteralKind.DEFAULT,
      getPrecisionScaleFacets(0, null), EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED);
  expectErrorInValueOfString(instance, "2012-02-29T23:32:02.98700", EdmLiteralKind.DEFAULT,
      getPrecisionScaleFacets(2, null), EdmSimpleTypeException.LITERAL_FACETS_NOT_MATCHED);
  expectErrorInValueOfString(instance, "2012-02-29T23:32:02.9876", EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "2012-02-29T23:32:02.", EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "2012-02-29T23:32:02.0000000000", EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "20120229T233202", EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "1900-02-29T00:00:00", EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "2012-02-29T24:00:01", EdmLiteralKind.DEFAULT, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "\\/Date(1)\\/", EdmLiteralKind.JSON, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "/Date(12345678901234567890)/", EdmLiteralKind.JSON, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "/Date(1330558323000+0060)/", EdmLiteralKind.JSON, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "datetime'2012-02-29T23:32:02+01:00'", EdmLiteralKind.URI, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "date'2012-02-29T23:32:02'", EdmLiteralKind.URI, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "datetime'2012-02-29T23:32:02", EdmLiteralKind.URI, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);
  expectErrorInValueOfString(instance, "datetime'", EdmLiteralKind.URI, null,
      EdmSimpleTypeException.LITERAL_ILLEGAL_CONTENT);

  expectTypeErrorInValueOfString(instance, "2012-02-29T23:32", EdmLiteralKind.DEFAULT);
  expectTypeErrorInValueOfString(instance, "/Date(1)/", EdmLiteralKind.JSON);
}
 
Example 11
Source File: TimestampTests.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = IllegalArgumentException.class)
public void test39() throws Exception {
    int nanos = 999999999;
    Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
    ts1.setNanos(nanos + 1);
}
 
Example 12
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 13
Source File: TimeUtil.java    From Komondor with GNU General Public License v3.0 4 votes vote down vote up
final static Timestamp fastTimestampCreate(boolean useGmtConversion, Calendar gmtCalIfNeeded, Calendar cal, int year, int month, int day, int hour,
        int minute, int seconds, int secondsPart) {

    synchronized (cal) {
        java.util.Date origCalDate = cal.getTime();
        try {
            cal.clear();

            // why-oh-why is this different than java.util.date, in the year part, but it still keeps the silly '0' for the start month????
            cal.set(year, month - 1, day, hour, minute, seconds);

            int offsetDiff = 0;

            if (useGmtConversion) {
                int fromOffset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);

                if (gmtCalIfNeeded == null) {
                    gmtCalIfNeeded = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
                }
                gmtCalIfNeeded.clear();

                gmtCalIfNeeded.setTimeInMillis(cal.getTimeInMillis());

                int toOffset = gmtCalIfNeeded.get(Calendar.ZONE_OFFSET) + gmtCalIfNeeded.get(Calendar.DST_OFFSET);
                offsetDiff = fromOffset - toOffset;
            }

            if (secondsPart != 0) {
                cal.set(Calendar.MILLISECOND, secondsPart / 1000000);
            }

            long tsAsMillis = cal.getTimeInMillis();

            Timestamp ts = new Timestamp(tsAsMillis + offsetDiff);

            ts.setNanos(secondsPart);

            return ts;
        } finally {
            cal.setTime(origCalDate);
        }
    }
}
 
Example 14
Source File: JavatimeTest.java    From openjdk-jdk9 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: java_sql_Timestamp.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
protected Timestamp getObject() {
    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    timestamp.setNanos(1 + timestamp.getNanos());
    return timestamp;
}
 
Example 16
Source File: TestChangeColumn.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests making an indexed column no longer auto increment.
 */
public void testUniqeIndexColumnUnmakeAutoIncrement()
{
    if (!getPlatformInfo().isIndicesSupported() ||
        !getPlatformInfo().isNonPrimaryKeyIdentityColumnsSupported())
    {
        return;
    }

    boolean      isSybase  = SybasePlatform.DATABASENAME.equals(getPlatform().getName());
    final String model1Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        (isSybase ?
            "    <column name='avalue1' type='NUMERIC' size='12,0' autoIncrement='true'/>\n"
          : "    <column name='avalue1' type='INTEGER' autoIncrement='true'/>\n") +
        "    <column name='avalue2' type='TIMESTAMP'/>\n"+
        "    <unique name='testindex'>\n"+
        "      <unique-column name='avalue1'/>\n"+
        "      <unique-column name='avalue2'/>\n"+
        "    </unique>\n"+
        "  </table>\n"+
        "</database>";
    final String model2Xml = 
        "<?xml version='1.0' encoding='ISO-8859-1'?>\n"+
        "<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='roundtriptest'>\n"+
        "  <table name='roundtrip'>\n"+
        "    <column name='pk' type='INTEGER' primaryKey='true' required='true'/>\n"+
        (isSybase ?
            "    <column name='avalue1' type='NUMERIC' size='12,0'/>\n"
          : "    <column name='avalue1' type='INTEGER'/>\n") +
        "    <column name='avalue2' type='TIMESTAMP'/>\n"+
        "    <unique name='testindex'>\n"+
        "      <unique-column name='avalue1'/>\n"+
        "      <unique-column name='avalue2'/>\n"+
        "    </unique>\n"+
        "  </table>\n"+
        "</database>";

    createDatabase(model1Xml);

    Timestamp time  = new Timestamp(new Date().getTime());

    // to avoid problems with the database's time resolution
    time.setNanos(0);

    insertRow("roundtrip", new Object[] { new Integer(1), null, time });

    alterDatabase(model2Xml);

    assertEquals(getAdjustedModel(),
                 readModelFromDatabase("roundtriptest"));

    List   beans = getRows("roundtrip");
    Object value = isSybase ? (Object)new BigDecimal("1") : new Integer(1);

    assertEquals(new Integer(1), beans.get(0), "pk");
    assertEquals(value,          beans.get(0), "avalue1");
    assertEquals(time,           beans.get(0), "avalue2");
}
 
Example 17
Source File: CloudSpannerPreparedStatement.java    From spanner-jdbc with MIT License 4 votes vote down vote up
/**
 * 
 * @return A timestamp value which automatically will be converted into the commit timestamp in
 *         Cloud Spanner when set as a parameter for a {@link PreparedStatement}
 */
public static Timestamp getSpannerCommitTimestamp() {
  Timestamp res = new Timestamp(SPANNER_COMMIT_TIMESTAMP.getTime());
  res.setNanos(SPANNER_COMMIT_TIMESTAMP.getNanos());
  return res;
}
 
Example 18
Source File: OrcBatchReader.java    From flink with Apache License 2.0 4 votes vote down vote up
private static Timestamp readTimestamp(long time, int nanos) {
	Timestamp ts = new Timestamp(time);
	ts.setNanos(nanos);
	return ts;
}
 
Example 19
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 20
Source File: OrcBatchReader.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private static Timestamp readTimestamp(long time, int nanos) {
	Timestamp ts = new Timestamp(time);
	ts.setNanos(nanos);
	return ts;
}