本文整理汇总了Java中java.sql.Timestamp.setNanos方法的典型用法代码示例。如果您正苦于以下问题:Java Timestamp.setNanos方法的具体用法?Java Timestamp.setNanos怎么用?Java Timestamp.setNanos使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.sql.Timestamp
的用法示例。
在下文中一共展示了Timestamp.setNanos方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testBug21438
import java.sql.Timestamp; //导入方法依赖的package包/类
/**
* Tests BUG#21438, server-side PS fails when using jdbcCompliantTruncation.
* If either is set to FALSE (&useServerPrepStmts=false or
* &jdbcCompliantTruncation=false) test succedes.
*
* @throws Exception
* if the test fails.
*/
@SuppressWarnings("deprecation")
public void testBug21438() throws Exception {
createTable("testBug21438", "(t_id int(10), test_date timestamp NOT NULL,primary key t_pk (t_id));");
assertEquals(1, this.stmt.executeUpdate("insert into testBug21438 values (1,NOW());"));
if (this.versionMeetsMinimum(4, 1)) {
this.pstmt = ((com.mysql.jdbc.Connection) this.conn)
.serverPrepareStatement("UPDATE testBug21438 SET test_date=ADDDATE(?,INTERVAL 1 YEAR) WHERE t_id=1;");
Timestamp ts = new Timestamp(System.currentTimeMillis());
ts.setNanos(999999999);
this.pstmt.setTimestamp(1, ts);
assertEquals(1, this.pstmt.executeUpdate());
Timestamp future = (Timestamp) getSingleIndexedValueWithQuery(1, "SELECT test_date FROM testBug21438");
assertEquals(future.getYear() - ts.getYear(), 1);
}
}
示例2: fastTimestampCreate
import java.sql.Timestamp; //导入方法依赖的package包/类
final static Timestamp fastTimestampCreate(TimeZone tz, int year, int month, int day, int hour, int minute, int seconds, int secondsPart) {
Calendar cal = (tz == null) ? new GregorianCalendar() : new GregorianCalendar(tz);
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);
long tsAsMillis = cal.getTimeInMillis();
Timestamp ts = new Timestamp(tsAsMillis);
ts.setNanos(secondsPart);
return ts;
}
示例3: changeTimezone
import java.sql.Timestamp; //导入方法依赖的package包/类
/**
* Change the given timestamp from one timezone to another
*
* @param conn
* the current connection to the MySQL server
* @param tstamp
* the timestamp to change
* @param fromTz
* the timezone to change from
* @param toTz
* the timezone to change to
*
* @return the timestamp changed to the timezone 'toTz'
*/
public static Timestamp changeTimezone(MySQLConnection conn, Calendar sessionCalendar, Calendar targetCalendar, Timestamp tstamp, TimeZone fromTz,
TimeZone toTz, boolean rollForward) {
if ((conn != null)) {
if (conn.getUseTimezone()) {
// Convert the timestamp from GMT to the server's timezone
Calendar fromCal = Calendar.getInstance(fromTz);
fromCal.setTime(tstamp);
int fromOffset = fromCal.get(Calendar.ZONE_OFFSET) + fromCal.get(Calendar.DST_OFFSET);
Calendar toCal = Calendar.getInstance(toTz);
toCal.setTime(tstamp);
int toOffset = toCal.get(Calendar.ZONE_OFFSET) + toCal.get(Calendar.DST_OFFSET);
int offsetDiff = fromOffset - toOffset;
long toTime = toCal.getTime().getTime();
if (rollForward) {
toTime += offsetDiff;
} else {
toTime -= offsetDiff;
}
Timestamp changedTimestamp = new Timestamp(toTime);
return changedTimestamp;
} else if (conn.getUseJDBCCompliantTimezoneShift()) {
if (targetCalendar != null) {
Timestamp adjustedTimestamp = new Timestamp(jdbcCompliantZoneShift(sessionCalendar, targetCalendar, tstamp));
adjustedTimestamp.setNanos(tstamp.getNanos());
return adjustedTimestamp;
}
}
}
return tstamp;
}
示例4: test40
import java.sql.Timestamp; //导入方法依赖的package包/类
@Test
public void test40() throws Exception {
int nanos = 999999999;
Timestamp ts1 = Timestamp.valueOf("1961-08-30 00:00:00");
ts1.setNanos(nanos);
assertTrue(ts1.getNanos() == nanos, "Error Invalid Nanos value");
}
示例5: truncateFractionalSeconds
import java.sql.Timestamp; //导入方法依赖的package包/类
public static Timestamp truncateFractionalSeconds(Timestamp timestamp) {
Timestamp truncatedTimestamp = new Timestamp(timestamp.getTime());
truncatedTimestamp.setNanos(0);
return truncatedTimestamp;
}
示例6: enforceSize
import java.sql.Timestamp; //导入方法依赖的package包/类
/**
* Check an object for type CHAR and VARCHAR and truncate/pad based on
* the size
*
* @param obj object to check
* @param type the object type
* @param size size to enforce
* @param check throw if too long
* @return the altered object if the right type, else the object
* passed in unaltered
* @throws HsqlException if data too long
*/
static Object enforceSize(Object obj, int type, int size, int scale,
boolean check) throws HsqlException {
if (obj == null) {
return obj;
}
if (size == 0 && type != Types.TIMESTAMP) {
return obj;
}
// todo: need to handle BINARY like this as well
switch (type) {
case Types.CHAR :
return checkChar((String) obj, size, check);
case Types.VARCHAR :
case Types.VARCHAR_IGNORECASE :
return checkVarchar((String) obj, size, check);
case Types.NUMERIC :
case Types.DECIMAL :
BigDecimal dec = (BigDecimal) obj;
dec = dec.setScale(scale, BigDecimal.ROUND_HALF_DOWN);
BigInteger big = JavaSystem.getUnscaledValue(dec);
int sign = big.signum() == -1 ? 1
: 0;
if (big.toString().length() - sign > size) {
throw Trace.error(Trace.STRING_DATA_TRUNCATION);
}
return dec;
case Types.TIMESTAMP :
if (size == 6) {
return obj;
}
Timestamp ts = (Timestamp) obj;
int nanos = ts.getNanos();
int divisor = tenPower[size];
int newNanos = (nanos / divisor) * divisor;
ts.setNanos(newNanos);
return ts;
default :
return obj;
}
}
示例7: test
import java.sql.Timestamp; //导入方法依赖的package包/类
public void test() throws Exception {
try {
Connection conn = newConnection();
Statement stmt = conn.createStatement();
stmt.executeUpdate("drop table testA if exists;");
stmt.executeUpdate("create table testA(data timestamp);");
TimeZone pst = TimeZone.getTimeZone("PST");
Calendar cal = new GregorianCalendar(pst);
cal.clear();
cal.set(2005, 0, 1, 0, 0, 0);
// yyyy-mm-dd hh:mm:ss.fffffffff
Timestamp ts = new Timestamp(cal.getTimeInMillis());
ts.setNanos(1000);
PreparedStatement ps =
conn.prepareStatement("insert into testA values(?)");
ps.setTimestamp(1, ts, cal);
ps.execute();
ps.setTimestamp(1, ts, null);
ps.execute();
String sql = "select * from testA";
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
rs.next();
Timestamp returned = rs.getTimestamp(1, cal);
rs.next();
Timestamp def = rs.getTimestamp(1, null);
assertEquals(ts, returned);
assertEquals(ts, def);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
示例8: main
import java.sql.Timestamp; //导入方法依赖的package包/类
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!");
}
示例9: fastTimestampCreate
import java.sql.Timestamp; //导入方法依赖的package包/类
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);
}
}
}
示例10: getObject
import java.sql.Timestamp; //导入方法依赖的package包/类
protected Timestamp getObject() {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
timestamp.setNanos(1 + timestamp.getNanos());
return timestamp;
}
示例11: getTimestamp
import java.sql.Timestamp; //导入方法依赖的package包/类
/**
* <!-- start generic documentation -->
* Retrieves the value of the designated column in the current row
* of this <code>ResultSet</code> object as a <code>java.sql.Timestamp</code> object
* in the Java programming language.
* This method uses the given calendar to construct an appropriate millisecond
* value for the timestamp if the underlying database does not store
* timezone information.
* <!-- end generic documentation -->
* <h3>HSQLDB-Specific Information:</h3> <p>
*
* The JDBC specification for this method is vague. HSQLDB interprets the
* specification as follows:
*
* <ol>
* <li>If the SQL type of the column is WITH TIME ZONE, then the UTC value
* of the returned java.sql.Timestamp object is the UTC of the SQL value
* without modification. In other words, the Calendar object is not used.
* </li>
* <li>If the SQL type of the column is WITHOUT TIME ZONE, then the
* UTC value of the returned java.sql.Timestamp will represent the correct
* timestamp for the time zone (including daylight saving time) of the given
* Calendar object. </li>
* <li>In this case, if the cal argument is null, then the default Calendar
* of the JVM is used, which results in the same Object as one returned by the
* getTimestamp() methods without the Calendar parameter.</li>
* </ol>
* </div>
*
* @param columnIndex the first column is 1, the second is 2, ...
* @param cal the <code>java.util.Calendar</code> object
* to use in constructing the timestamp
* @return the column value as a <code>java.sql.Timestamp</code> object;
* if the value is SQL <code>NULL</code>,
* the value returned is <code>null</code> in the Java programming language
* @exception SQLException if a database access error occurs
* or this method is called on a closed result set
* @since JDK 1.2 (JDK 1.1.x developers: read the overview for
* JDBCResultSet)
*/
public Timestamp getTimestamp(int columnIndex,
Calendar cal) throws SQLException {
TimestampData t = (TimestampData) getColumnInType(columnIndex,
Type.SQL_TIMESTAMP);
if (t == null) {
return null;
}
long millis = t.getSeconds() * 1000;
if (!resultMetaData.columnTypes[--columnIndex]
.isDateTimeTypeWithZone()) {
Calendar calendar = cal == null ? session.getCalendar()
: cal;
if (cal != null) {
millis = HsqlDateTime.convertMillisToCalendar(calendar,
millis);
}
}
Timestamp ts = new Timestamp(millis);
ts.setNanos(t.getNanos());
return ts;
}
示例12: getTimestamp
import java.sql.Timestamp; //导入方法依赖的package包/类
/**
* <!-- start generic documentation -->
*
* Retrieves the value of the designated JDBC <code>TIMESTAMP</code> parameter as a
* <code>java.sql.Timestamp</code> object, using
* the given <code>Calendar</code> object to construct
* the <code>Timestamp</code> object.
* With a <code>Calendar</code> object, the driver
* can calculate the timestamp taking into account a custom timezone and locale.
* If no <code>Calendar</code> object is specified, the driver uses the
* default timezone and locale.
*
* <!-- end generic documentation -->
*
* <!-- start release-specific documentation -->
* <div class="ReleaseSpecificDocumentation">
* <h3>HSQLDB-Specific Information:</h3> <p>
*
* HSQLDB supports this feature. <p>
*
* </div>
* <!-- end release-specific documentation -->
*
* @param parameterIndex the first parameter is 1, the second is 2,
* and so on
* @param cal the <code>Calendar</code> object the driver will use
* to construct the timestamp
* @return the parameter value. If the value is SQL <code>NULL</code>, the result
* is <code>null</code>.
* @exception SQLException if a database access error occurs or
* this method is called on a closed <code>CallableStatement</code>
* @see #setTimestamp
* @since JDK 1.2 (JDK 1.1.x developers: read the overview for
* JDBCParameterMetaData)
*/
public synchronized Timestamp getTimestamp(int parameterIndex,
Calendar cal) throws SQLException {
TimestampData t = (TimestampData) getColumnInType(parameterIndex,
Type.SQL_TIMESTAMP);
if (t == null) {
return null;
}
long millis = t.getSeconds() * 1000;
if (parameterTypes[--parameterIndex].isDateTimeTypeWithZone()) {}
else {
// UTC - calZO == (UTC - sessZO) + (sessionZO - calZO)
if (cal != null) {
int zoneOffset = HsqlDateTime.getZoneMillis(cal, millis);
millis += session.getZoneSeconds() * 1000 - zoneOffset;
}
}
Timestamp ts = new Timestamp(millis);
ts.setNanos(t.getNanos());
return ts;
}
示例13: timestampValue
import java.sql.Timestamp; //导入方法依赖的package包/类
/**
* @param time milliseconds
* @param nano nanoseconds
* @return Timestamp object
*/
public static Timestamp timestampValue(long time, int nano) {
Timestamp ts = new Timestamp(time);
ts.setNanos(nano);
return ts;
}