Example usage for java.sql Timestamp setNanos

List of usage examples for java.sql Timestamp setNanos

Introduction

In this page you can find the example usage for java.sql Timestamp setNanos.

Prototype

public void setNanos(int n) 

Source Link

Document

Sets this Timestamp object's nanos field to the given value.

Usage

From source file:org.apache.hadoop.hive.ql.exec.TestUtilities.java

public void testSerializeTimestamp() {
    Timestamp ts = new Timestamp(1374554702000L);
    ts.setNanos(123456);
    ExprNodeConstantDesc constant = new ExprNodeConstantDesc(ts);
    List<ExprNodeDesc> children = new ArrayList<ExprNodeDesc>(1);
    children.add(constant);//from  www  .j  a  va 2 s  . c  o  m
    ExprNodeGenericFuncDesc desc = new ExprNodeGenericFuncDesc(TypeInfoFactory.timestampTypeInfo,
            new GenericUDFFromUtcTimestamp(), children);
    assertEquals(desc.getExprString(),
            Utilities.deserializeExpression(Utilities.serializeExpression(desc)).getExprString());
}

From source file:org.apache.hadoop.hive.ql.udf.generic.GenericUDFFromUtcTimestamp.java

protected Timestamp applyOffset(long offset, Timestamp t) {
    long newTime = t.getTime() + offset;
    Timestamp t2 = new Timestamp(newTime);
    t2.setNanos(t.getNanos());

    return t2;/*www  .  j a  v  a 2  s  .  c om*/
}

From source file:org.apache.hadoop.hive.serde2.io.TimestampWritable.java

public static Timestamp decimalToTimestamp(BigDecimal d) {
    BigDecimal seconds = new BigDecimal(d.longValue());
    long millis = d.multiply(new BigDecimal(1000)).longValue();
    int nanos = d.subtract(seconds).multiply(new BigDecimal(1000000000)).intValue();

    Timestamp t = new Timestamp(millis);
    t.setNanos(nanos);

    return t;/*from ww w  .j a v  a 2 s. c  o m*/
}

From source file:org.apache.hadoop.hive.serde2.io.TimestampWritable.java

public static Timestamp doubleToTimestamp(double f) {
    long seconds = (long) f;

    // We must ensure the exactness of the double's fractional portion.
    // 0.6 as the fraction part will be converted to 0.59999... and
    // significantly reduce the savings from binary serializtion
    BigDecimal bd = new BigDecimal(String.valueOf(f));
    bd = bd.subtract(new BigDecimal(seconds)).multiply(new BigDecimal(1000000000));
    int nanos = bd.intValue();

    // Convert to millis
    long millis = seconds * 1000;
    Timestamp t = new Timestamp(millis);

    // Set remaining fractional portion to nanos
    t.setNanos(nanos);
    return t;/*from ww  w . ja  v a2  s. c o m*/
}

From source file:org.apache.hadoop.hive.serde2.io.TimestampWritable.java

public static void setTimestamp(Timestamp t, byte[] bytes, int offset) {
    boolean hasDecimal = hasDecimal(bytes[offset]);
    t.setTime(((long) TimestampWritable.getSeconds(bytes, offset)) * 1000);
    if (hasDecimal) {
        t.setNanos(TimestampWritable.getNanos(bytes, offset + 4));
    }/* w  ww . j a v  a2s . c o  m*/
}

From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java

/**
 * Set the given value as a parameter to the statement.
 *///from   w w  w  .  j a  v a  2 s .  c om
public void setTimestamp(PreparedStatement stmnt, int idx, Timestamp val, Calendar cal, Column col)
        throws SQLException {
    // ensure that we do not insert dates at a greater precision than
    // that at which they will be returned by a SELECT
    int rounded = (int) Math.round(val.getNanos() / (double) datePrecision);
    int nanos = rounded * datePrecision;
    if (nanos > 999999999) {
        // rollover to next second
        val.setTime(val.getTime() + 1000);
        nanos = 0;
    }

    Timestamp valForStmnt = new Timestamp(val.getTime());
    valForStmnt.setNanos(nanos);

    if (cal == null)
        stmnt.setTimestamp(idx, valForStmnt);
    else
        stmnt.setTimestamp(idx, valForStmnt, cal);
}

From source file:org.apache.openjpa.persistence.kernel.TestProxies2.java

public void testTimestamp() {
    OpenJPAEntityManager pm = getPM(true, true);
    startTx(pm);/* w w w  .j  a v  a  2  s.  c om*/
    ProxiesPC pc = pm.find(ProxiesPC.class, _oid);
    java.sql.Timestamp tstamp = pc.getTimestamp();
    assertNotNull(tstamp);

    // dates can lose precision, but make sure same day
    assertEquals(_timestamp.getYear(), tstamp.getYear());
    assertEquals(_timestamp.getMonth(), tstamp.getMonth());
    assertEquals(_timestamp.getDate(), tstamp.getDate());

    // make sure proxied
    assertTrue(!pm.isDirty(pc));
    tstamp.setNanos(100);
    assertTrue(pm.isDirty(pc));

    endTx(pm);
    assertEquals(tstamp, pc.getTimestamp());
    endEm(pm);
}

From source file:org.apache.phoenix.end2end.DateTimeIT.java

@Test
public void testProjectedDateTimestampUnequal() throws Exception {
    String tableName = generateUniqueName();
    String ddl = "CREATE TABLE IF NOT EXISTS " + tableName
            + " (k1 INTEGER PRIMARY KEY, dates DATE, timestamps TIMESTAMP)";
    conn.createStatement().execute(ddl);
    // Differ by date
    String dml = "UPSERT INTO " + tableName + " VALUES (1," + "TO_DATE('2004-02-04 00:10:10'),"
            + "TO_TIMESTAMP('2006-04-12 00:10:10'))";
    conn.createStatement().execute(dml);
    // Differ by time
    dml = "UPSERT INTO " + tableName + " VALUES (2," + "TO_DATE('2004-02-04 00:10:10'), "
            + "TO_TIMESTAMP('2004-02-04 15:10:20'))";
    conn.createStatement().execute(dml);
    // Differ by nanoseconds
    PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?, ?, ?)");
    stmt.setInt(1, 3);//from www  .j  a  va 2  s.  c o  m
    stmt.setDate(2, new Date(1000));
    Timestamp ts = new Timestamp(1000);
    ts.setNanos(100);
    stmt.setTimestamp(3, ts);
    stmt.execute();
    // Equality
    dml = "UPSERT INTO " + tableName + " VALUES (4," + "TO_DATE('2004-02-04 00:10:10'), "
            + "TO_TIMESTAMP('2004-02-04 00:10:10'))";
    conn.createStatement().execute(dml);
    conn.commit();

    ResultSet rs = conn.createStatement().executeQuery("SELECT dates = timestamps FROM " + tableName);
    assertTrue(rs.next());
    assertEquals(false, rs.getBoolean(1));
    assertTrue(rs.next());
    assertEquals(false, rs.getBoolean(1));
    assertTrue(rs.next());
    assertEquals(false, rs.getBoolean(1));
    assertTrue(rs.next());
    assertEquals(true, rs.getBoolean(1));
    assertFalse(rs.next());
}

From source file:org.apache.phoenix.end2end.DateTimeIT.java

@Test
public void testProjectedTimeTimestampCompare() throws Exception {
    String tableName = generateUniqueName();
    String ddl = "CREATE TABLE IF NOT EXISTS " + tableName
            + " (k1 INTEGER PRIMARY KEY, times TIME, timestamps TIMESTAMP)";
    conn.createStatement().execute(ddl);
    // Differ by date
    String dml = "UPSERT INTO " + tableName + " VALUES (1," + "TO_TIME('2004-02-04 00:10:10'),"
            + "TO_TIMESTAMP('2006-04-12 00:10:10'))";
    conn.createStatement().execute(dml);
    // Differ by time
    dml = "UPSERT INTO " + tableName + " VALUES (2," + "TO_TIME('2004-02-04 00:10:10'), "
            + "TO_TIMESTAMP('2004-02-04 15:10:20'))";
    conn.createStatement().execute(dml);
    // Differ by nanoseconds
    PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?, ?, ?)");
    stmt.setInt(1, 3);/* w ww  . ja va  2 s .  c o m*/
    stmt.setTime(2, new Time(1000));
    Timestamp ts = new Timestamp(1000);
    ts.setNanos(100);
    stmt.setTimestamp(3, ts);
    stmt.execute();
    // Equality
    dml = "UPSERT INTO " + tableName + " VALUES (4," + "TO_TIME('2004-02-04 00:10:10'), "
            + "TO_TIMESTAMP('2004-02-04 00:10:10'))";
    conn.createStatement().execute(dml);
    conn.commit();

    ResultSet rs = conn.createStatement().executeQuery("SELECT times = timestamps FROM " + tableName);
    assertTrue(rs.next());
    assertEquals(false, rs.getBoolean(1));
    assertTrue(rs.next());
    assertEquals(false, rs.getBoolean(1));
    assertTrue(rs.next());
    assertEquals(false, rs.getBoolean(1));
    assertTrue(rs.next());
    assertEquals(true, rs.getBoolean(1));
    assertFalse(rs.next());
}

From source file:org.apache.phoenix.end2end.DateTimeIT.java

@Test
public void testProjectedDateUnsignedTimestampCompare() throws Exception {
    String tableName = generateUniqueName();
    String ddl = "CREATE TABLE IF NOT EXISTS " + tableName
            + " (k1 INTEGER PRIMARY KEY, dates DATE, timestamps UNSIGNED_TIMESTAMP)";
    conn.createStatement().execute(ddl);
    // Differ by date
    String dml = "UPSERT INTO " + tableName + " VALUES (1," + "TO_DATE('2004-02-04 00:10:10'),"
            + "TO_TIMESTAMP('2006-04-12 00:10:10'))";
    conn.createStatement().execute(dml);
    // Differ by time
    dml = "UPSERT INTO " + tableName + " VALUES (2," + "TO_DATE('2004-02-04 00:10:10'), "
            + "TO_TIMESTAMP('2004-02-04 15:10:20'))";
    conn.createStatement().execute(dml);
    // Differ by nanoseconds
    PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?, ?, ?)");
    stmt.setInt(1, 3);/* w  ww  . ja va 2  s.com*/
    stmt.setDate(2, new Date(1000));
    Timestamp ts = new Timestamp(1000);
    ts.setNanos(100);
    stmt.setTimestamp(3, ts);
    stmt.execute();
    // Equality
    dml = "UPSERT INTO " + tableName + " VALUES (4," + "TO_DATE('2004-02-04 00:10:10'), "
            + "TO_TIMESTAMP('2004-02-04 00:10:10'))";
    conn.createStatement().execute(dml);
    conn.commit();

    ResultSet rs = conn.createStatement().executeQuery("SELECT dates = timestamps FROM " + tableName);
    assertTrue(rs.next());
    assertEquals(false, rs.getBoolean(1));
    assertTrue(rs.next());
    assertEquals(false, rs.getBoolean(1));
    assertTrue(rs.next());
    assertEquals(false, rs.getBoolean(1));
    assertTrue(rs.next());
    assertEquals(true, rs.getBoolean(1));
    assertFalse(rs.next());
}