Java Code Examples for java.sql.Timestamp.getTime()

The following are Jave code examples for showing how to use getTime() of the java.sql.Timestamp class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: wisp   File: EntryTimeFilter.java   View Source Code Vote up 6 votes
/**
 * 过滤掉LOG日志比当前时间小较多的数据, 避免长时间处理旧数据
 *
 * @param entry
 * @param iEntryFilterChain
 *
 * @throws WispProcessorException
 */
@Override
public void doFilter(MysqlEntryWrap entry, IEntryFilterChain iEntryFilterChain) throws WispProcessorException {

    Timestamp timestamp = new Timestamp(System.currentTimeMillis());
    long currentTime = timestamp.getTime();

    long logTime = entry.getMysqlEntry().getTime();

    // 5s
    if (logTime + 1000 * 5 < currentTime) {

        LOGGER.warn("ignore entry because of smaller time logTime:{}, currentTime:{}, entry{} ", logTime,
                currentTime, entry);

    } else {
        iEntryFilterChain.doFilter(entry);
    }
}
 
Example 2
Project: tangyuan2   File: DateTypeHandler.java   View Source Code Vote up 5 votes
@Override
public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
	Timestamp sqlTimestamp = cs.getTimestamp(columnIndex);
	if (sqlTimestamp != null) {
		return new Date(sqlTimestamp.getTime());
	}
	return null;
}
 
Example 3
Project: calcite-avatica   File: AbstractCursor.java   View Source Code Vote up 5 votes
@Override public Date getDate(Calendar calendar) throws SQLException {
  final Timestamp timestamp  = getTimestamp(calendar);
  if (timestamp == null) {
    return null;
  }
  return new Date(timestamp.getTime());
}
 
Example 4
Project: rapidminer   File: DatabaseDataSet.java   View Source Code Vote up 5 votes
public Date getDate(int columnIndex) throws ParseException {
    try {
        Timestamp e = DatabaseDataSet.this.resultSet.getTimestamp(this.getDatabaseColumnIndex(columnIndex));
        return DatabaseDataSet.this.resultSet.wasNull()?null:new Date(e.getTime());
    } catch (SQLException var3) {
        throw new ParseException(var3.getMessage(), var3);
    }
}
 
Example 5
Project: calcite-avatica   File: AbstractCursor.java   View Source Code Vote up 5 votes
@Override public Date getDate(Calendar calendar) throws SQLException {
  final Timestamp timestamp  = getTimestamp(calendar);
  if (timestamp == null) {
    return null;
  }
  return new Date(timestamp.getTime());
}
 
Example 6
Project: Pxls   File: DBUser.java   View Source Code Vote up 5 votes
@Override
public DBUser map(int index, ResultSet r, StatementContext ctx) throws SQLException {
    Timestamp stamp = r.getTimestamp("cooldown_expiry");
    Timestamp ban = r.getTimestamp("ban_expiry");
    return new DBUser(
            r.getInt("id"),
            r.getString("username"),
            r.getString("login"),
            stamp == null ? 0 : stamp.getTime(),
            Role.valueOf(r.getString("role")),
            ban == null ? 0 : ban.getTime()
    );
}
 
Example 7
Project: sumo   File: SarImageReader.java   View Source Code Vote up 5 votes
@Override
  public Date getImageDate(){
String ts=getTimeStampStart();
ts=ts.replace("Z", "");
  	Timestamp t=Timestamp.valueOf(ts);
  	return new Date(t.getTime());
  }
 
Example 8
Project: wisp   File: EntryTimeFilterTestCase.java   View Source Code Vote up 5 votes
@Test
public void testOk() {

    // Given logger
    LogbackCapturingAppender capturing = LogbackCapturingAppender.Factory.weaveInto(EntryTimeFilter.getLOGGER());

    IEntryFilter entryFilter = new EntryTimeFilter();

    List<IEntryFilter> filters = new ArrayList<>();
    filters.add(entryFilter);
    filters.add(entryFilter);
    IEntryFilterChain entryFilterChain = EntryFilterChainFactory.getEntryFilterChain(filters);

    try {

        MysqlEntry mysqlEntry = new MysqlEntry();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        long currentTime = timestamp.getTime();
        mysqlEntry.setTime(currentTime - 1000 * 2);
        entryFilterChain.doFilter(new MysqlEntryWrap("topic", mysqlEntry));

    } catch (WispProcessorException e) {
        Assert.assertTrue(false);
    }

    Assert.assertEquals(capturing.getCapturedLogMessage(), null);

}
 
Example 9
Project: SO_reputation   File: StackOverflowServices.java   View Source Code Vote up 5 votes
private String getDifference(Timestamp begin, Timestamp end)
{
	long diff = end.getTime() - begin.getTime();
	
	long diffSeconds = diff / 1000 % 60;
	long diffMinutes = diff / (60 * 1000) % 60;
	long diffHours = diff / (60 * 60 * 1000) % 24;
	long diffDays = diff / (24 * 60 * 60 * 1000);
	
	return diffDays + " days, " + diffHours + " hours, " + diffMinutes + " minutes, " + diffSeconds + " seconds";
}
 
Example 10
Project: openjdk-jdk10   File: TimestampTest.java   View Source Code Vote up 4 votes
/**
 * 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 11
Project: openjdk-jdk10   File: TimestampTests.java   View Source Code Vote up 4 votes
@Test
public void test28() {
    Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
    java.util.Date ts2 = new java.util.Date(ts1.getTime());
    assertTrue(ts1.compareTo(ts2) == 0, "Error ts1 != ts2");
}
 
Example 12
Project: openjdk-jdk10   File: TimestampTests.java   View Source Code Vote up 4 votes
@Test
public void test31() {
    Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
    Date d = new Date(ts1.getTime());
    assertFalse(ts1.equals(d), "Error ts1 != d");
}
 
Example 13
Project: sstore-soft   File: JDBCPreparedStatement.java   View Source Code Vote up 4 votes
/**
 * <!-- start generic documentation -->
 * Sets the designated parameter to the given <code>java.sql.Timestamp</code> value,
 * using the given <code>Calendar</code> object.  The driver uses
 * the <code>Calendar</code> object to construct an SQL <code>TIMESTAMP</code> value,
 * which the driver then sends to the database.  With a
 *  <code>Calendar</code> object, the driver can calculate the timestamp
 * taking into account a custom timezone.  If no
 * <code>Calendar</code> object is specified, the driver uses the default
 * timezone, which is that of the virtual machine running the application.
 * <!-- end generic documentation -->
 * <div class="ReleaseSpecificDocumentation">
 * <h3>HSQLDB-Specific Information:</h3> <p>
 *
 * When a setXXX method is used to set a parameter of type
 * TIMESTAMP WITH TIME ZONE or TIME WITH TIME ZONE the time zone (including
 * Daylight Saving Time) of the Calendar is used as time zone.<p>
 *
 * When this method is used to set a parameter of type TIME or
 * TIME WITH TIME ZONE, then the nanosecond value of the Timestamp object
 * is used if the TIME parameter accepts fractional seconds.
 *
 * </div>
 * <!-- end release-specific documentation -->
 *
 * @param parameterIndex the first parameter is 1, the second is 2, ...
 * @param x the parameter value
 * @param cal the <code>Calendar</code> object the driver will use
 *            to construct the timestamp
 * @exception SQLException if a database access error occurs or
 * this method is called on a closed <code>PreparedStatement</code>
 * @since JDK 1.2 (JDK 1.1.x developers: read the overview for
 *   JDBCParameterMetaData)
 */
public synchronized void setTimestamp(int parameterIndex, Timestamp x,
        Calendar cal) throws SQLException {

    checkSetParameterIndex(parameterIndex, false);

    int i = parameterIndex - 1;

    if (x == null) {
        parameterValues[i] = null;

        return;
    }

    Type outType    = parameterTypes[i];
    long millis     = x.getTime();
    int  zoneOffset = 0;

    if (cal != null) {
        zoneOffset = HsqlDateTime.getZoneMillis(cal, millis);
    }

    switch (outType.typeCode) {

        case Types.SQL_TIMESTAMP :
            millis     += zoneOffset;
            zoneOffset = 0;

        // fall through
        case Types.SQL_TIMESTAMP_WITH_TIME_ZONE :
            parameterValues[i] = new TimestampData(millis / 1000,
                    x.getNanos(), zoneOffset / 1000);

            break;
        case Types.SQL_TIME :
            millis     += zoneOffset;
            zoneOffset = 0;

        // fall through
        case Types.SQL_TIME_WITH_TIME_ZONE :
            parameterValues[i] = new TimeData((int) (millis / 1000),
                    x.getNanos(), zoneOffset / 1000);

            break;
        default :
            throw Util.sqlException(ErrorCode.X_42561);
    }
}
 
Example 14
Project: openjdk-jdk10   File: TimestampTests.java   View Source Code Vote up 4 votes
@Test
public void test30() {
    Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
    java.util.Date d = new Timestamp(ts1.getTime());
    assertTrue(ts1.equals(d), "Error ts1 != d");
}
 
Example 15
Project: BibliotecaPS   File: StatementRegressionTest.java   View Source Code Vote up 4 votes
/**
 * Tests fix for BUG#5874, timezone correction goes in wrong 'direction' (when useTimezone=true and server timezone differs from client timezone).
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug5874() throws Exception {
    TimeZone defaultTimezone = TimeZone.getDefault();

    try {
        String clientTimezoneName = "America/Los_Angeles";
        String serverTimezoneName = "America/Chicago";

        TimeZone.setDefault(TimeZone.getTimeZone(clientTimezoneName));

        long clientTimezoneOffsetMillis = TimeZone.getDefault().getRawOffset();
        long serverTimezoneOffsetMillis = TimeZone.getTimeZone(serverTimezoneName).getRawOffset();

        long offsetDifference = clientTimezoneOffsetMillis - serverTimezoneOffsetMillis;

        SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

        long pointInTime = timestampFormat.parse("2004-10-04 09:19:00").getTime();

        Properties props = new Properties();
        props.put("useTimezone", "true");
        props.put("serverTimezone", serverTimezoneName);
        props.put("cacheDefaultTimezone", "false");

        Connection tzConn = getConnectionWithProps(props);
        Statement tzStmt = tzConn.createStatement();
        createTable("testBug5874", "(tstamp DATETIME, t TIME)");

        PreparedStatement tsPstmt = tzConn.prepareStatement("INSERT INTO testBug5874 VALUES (?, ?)");

        tsPstmt.setTimestamp(1, new Timestamp(pointInTime));
        tsPstmt.setTime(2, new Time(pointInTime));
        tsPstmt.executeUpdate();

        this.rs = tzStmt.executeQuery("SELECT * from testBug5874");

        while (this.rs.next()) { // Driver now converts/checks DATE/TIME/TIMESTAMP/DATETIME types when calling getString()...
            String retrTimestampString = new String(this.rs.getBytes(1));
            Timestamp retrTimestamp = this.rs.getTimestamp(1);

            java.util.Date timestampOnServer = timestampFormat.parse(retrTimestampString);

            long retrievedOffsetForTimestamp = retrTimestamp.getTime() - timestampOnServer.getTime();

            assertEquals("Original timestamp and timestamp retrieved using client timezone are not the same", offsetDifference,
                    retrievedOffsetForTimestamp);

            String retrTimeString = new String(this.rs.getBytes(2));
            Time retrTime = this.rs.getTime(2);

            java.util.Date timeOnServerAsDate = timeFormat.parse(retrTimeString);
            Time timeOnServer = new Time(timeOnServerAsDate.getTime());

            long retrievedOffsetForTime = retrTime.getTime() - timeOnServer.getTime();

            assertEquals("Original time and time retrieved using client timezone are not the same", offsetDifference, retrievedOffsetForTime);
        }

        tzConn.close();
    } finally {
        TimeZone.setDefault(defaultTimezone);
    }
}
 
Example 16
Project: s-store   File: JDBCPreparedStatement.java   View Source Code Vote up 4 votes
/**
 * <!-- start generic documentation -->
 * Sets the designated parameter to the given <code>java.sql.Timestamp</code> value,
 * using the given <code>Calendar</code> object.  The driver uses
 * the <code>Calendar</code> object to construct an SQL <code>TIMESTAMP</code> value,
 * which the driver then sends to the database.  With a
 *  <code>Calendar</code> object, the driver can calculate the timestamp
 * taking into account a custom timezone.  If no
 * <code>Calendar</code> object is specified, the driver uses the default
 * timezone, which is that of the virtual machine running the application.
 * <!-- end generic documentation -->
 * <div class="ReleaseSpecificDocumentation">
 * <h3>HSQLDB-Specific Information:</h3> <p>
 *
 * When a setXXX method is used to set a parameter of type
 * TIMESTAMP WITH TIME ZONE or TIME WITH TIME ZONE the time zone (including
 * Daylight Saving Time) of the Calendar is used as time zone.<p>
 *
 * When this method is used to set a parameter of type TIME or
 * TIME WITH TIME ZONE, then the nanosecond value of the Timestamp object
 * is used if the TIME parameter accepts fractional seconds.
 *
 * </div>
 * <!-- end release-specific documentation -->
 *
 * @param parameterIndex the first parameter is 1, the second is 2, ...
 * @param x the parameter value
 * @param cal the <code>Calendar</code> object the driver will use
 *            to construct the timestamp
 * @exception SQLException if a database access error occurs or
 * this method is called on a closed <code>PreparedStatement</code>
 * @since JDK 1.2 (JDK 1.1.x developers: read the overview for
 *   JDBCParameterMetaData)
 */
public synchronized void setTimestamp(int parameterIndex, Timestamp x,
        Calendar cal) throws SQLException {

    checkSetParameterIndex(parameterIndex, false);

    int i = parameterIndex - 1;

    if (x == null) {
        parameterValues[i] = null;

        return;
    }

    Type outType    = parameterTypes[i];
    long millis     = x.getTime();
    int  zoneOffset = 0;

    if (cal != null) {
        zoneOffset = HsqlDateTime.getZoneMillis(cal, millis);
    }

    switch (outType.typeCode) {

        case Types.SQL_TIMESTAMP :
            millis     += zoneOffset;
            zoneOffset = 0;

        // fall through
        case Types.SQL_TIMESTAMP_WITH_TIME_ZONE :
            parameterValues[i] = new TimestampData(millis / 1000,
                    x.getNanos(), zoneOffset / 1000);

            break;
        case Types.SQL_TIME :
            millis     += zoneOffset;
            zoneOffset = 0;

        // fall through
        case Types.SQL_TIME_WITH_TIME_ZONE :
            parameterValues[i] = new TimeData((int) (millis / 1000),
                    x.getNanos(), zoneOffset / 1000);

            break;
        default :
            throw Util.sqlException(ErrorCode.X_42561);
    }
}
 
Example 17
Project: jdk8u-jdk   File: TimestampTests.java   View Source Code Vote up 4 votes
@Test
public void test28() {
    Timestamp ts1 = Timestamp.valueOf("1966-08-30 08:08:08");
    java.util.Date ts2 = new java.util.Date(ts1.getTime());
    assertTrue(ts1.compareTo(ts2) == 0, "Error ts1 != ts2");
}
 
Example 18
Project: kite   File: TemporalPeriod.java   View Source Code Vote up 4 votes
public boolean overlap(Timestamp timestamp) {
    return (from()==null || timestamp.getTime() >= this.from().getTime()) &&
    (to()==null || timestamp.getTime() <= this.to().getTime());
}
 
Example 19
Project: lams   File: TimeUtil.java   View Source Code Vote up 4 votes
public static Timestamp truncateFractionalSeconds(Timestamp timestamp) {
    Timestamp truncatedTimestamp = new Timestamp(timestamp.getTime());
    truncatedTimestamp.setNanos(0);
    return truncatedTimestamp;
}
 
Example 20
Project: bluegreen-manager   File: TaskHistory.java   View Source Code Vote up 4 votes
public void setEndTime(Timestamp endTime)
{
  this.endTime = endTime == null ? null : new Timestamp(endTime.getTime());
}