本文整理汇总了Java中java.sql.Timestamp.getNanos方法的典型用法代码示例。如果您正苦于以下问题:Java Timestamp.getNanos方法的具体用法?Java Timestamp.getNanos怎么用?Java Timestamp.getNanos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.sql.Timestamp
的用法示例。
在下文中一共展示了Timestamp.getNanos方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toString
import java.sql.Timestamp; //导入方法依赖的package包/类
@Override
public String toString(final Object obj) {
final Timestamp timestamp = (Timestamp)obj;
final StringBuilder buffer = new StringBuilder(format.format(timestamp)).append('.');
if (timestamp.getNanos() == 0) {
buffer.append('0');
} else {
final String nanos = String.valueOf(timestamp.getNanos() + 1000000000);
int last = 10;
while (last > 2 && nanos.charAt(last - 1) == '0') {
--last;
}
buffer.append(nanos.subSequence(1, last));
}
return buffer.toString();
}
示例2: getByteValue
import java.sql.Timestamp; //导入方法依赖的package包/类
public byte[] getByteValue(ResultSet resultSet, int nCol1Based, boolean bEbcdicOutput)
{
try
{
Timestamp ts = resultSet.getTimestamp(nCol1Based);
String csValue = new DateUtil("yyyy-MM-dd-HH.mm.ss.", new java.util.Date(ts.getTime())).toString();
Integer intNanos = new Integer(ts.getNanos()/1000);
String csNano = intNanos.toString();
String csNanoPadded = StringUtil.leftPad(csNano, 6, '0');
csValue += csNanoPadded;
byte[] aBytes = csValue.getBytes();
if(bEbcdicOutput) // Must outout in ebcdic
AsciiEbcdicConverter.swapByteAsciiToEbcdic(aBytes, 0, aBytes.length);
return aBytes;
}
catch (SQLException e)
{
return null;
}
}
示例3: compare
import java.sql.Timestamp; //导入方法依赖的package包/类
static int compare(Timestamp a, Timestamp b) {
long atime = a.getTime();
long btime = b.getTime();
if (atime == btime) {
if (a.getNanos() == b.getNanos()) {
return 0;
}
return a.getNanos() > b.getNanos() ? 1
: -1;
}
return atime > btime ? 1
: -1;
}
示例4: isEqual
import java.sql.Timestamp; //导入方法依赖的package包/类
private static boolean isEqual(LocalDateTime ldt, Timestamp ts) {
ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
return zdt.getYear() == ts.getYear() + 1900 &&
zdt.getMonthValue() == ts.getMonth() + 1 &&
zdt.getDayOfMonth() == ts.getDate() &&
zdt.getHour() == ts.getHours() &&
zdt.getMinute() == ts.getMinutes() &&
zdt.getSecond() == ts.getSeconds() &&
zdt.getNano() == ts.getNanos();
}
示例5: main
import java.sql.Timestamp; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 1000; i++) {
Date d = new Date();
Timestamp ts = new Timestamp(d.getTime());
if (d.after(ts)) {
throw new RuntimeException("date with time " + d.getTime()
+ " should not be after TimeStamp , Nanos component of "
+ "TimeStamp is " +ts.getNanos());
}
Thread.sleep(1);
}
}
示例6: getTimestampString
import java.sql.Timestamp; //导入方法依赖的package包/类
public static String getTimestampString(Timestamp x,
Calendar cal) throws Exception {
synchronized (sdfts) {
sdfts.setCalendar(cal == null ? tempCalDefault
: cal);
String s = sdfts.format(new java.util.Date(x.getTime()))
+ x.getNanos();
return s + zerodatetime.substring(s.length());
}
}
示例7: setTimestamp
import java.sql.Timestamp; //导入方法依赖的package包/类
/**
* <!-- 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);
}
}
示例8: setTimestampInternal
import java.sql.Timestamp; //导入方法依赖的package包/类
/**
* Set a parameter to a java.sql.Timestamp value. The driver converts this
* to a SQL TIMESTAMP value when it sends it to the database.
*
* @param parameterIndex
* the first parameter is 1, the second is 2, ...
* @param x
* the parameter value
* @param tz
* the timezone to use
*
* @throws SQLException
* if a database-access error occurs.
*/
private void setTimestampInternal(int parameterIndex, Timestamp x, Calendar targetCalendar, TimeZone tz, boolean rollForward) throws SQLException {
if (x == null) {
setNull(parameterIndex, java.sql.Types.TIMESTAMP);
} else {
checkClosed();
if (!this.sendFractionalSeconds) {
x = TimeUtil.truncateFractionalSeconds(x);
}
if (!this.useLegacyDatetimeCode) {
newSetTimestampInternal(parameterIndex, x, targetCalendar);
} else {
Calendar sessionCalendar = this.connection.getUseJDBCCompliantTimezoneShift() ? this.connection.getUtcCalendar()
: getCalendarInstanceForSessionOrNew();
x = TimeUtil.changeTimezone(this.connection, sessionCalendar, targetCalendar, x, tz, this.connection.getServerTimezoneTZ(), rollForward);
if (this.connection.getUseSSPSCompatibleTimezoneShift()) {
doSSPSCompatibleTimezoneShift(parameterIndex, x);
} else {
synchronized (this) {
if (this.tsdf == null) {
this.tsdf = new SimpleDateFormat("''yyyy-MM-dd HH:mm:ss", Locale.US);
}
StringBuffer buf = new StringBuffer();
buf.append(this.tsdf.format(x));
if (this.serverSupportsFracSecs) {
int nanos = x.getNanos();
if (nanos != 0) {
buf.append('.');
buf.append(TimeUtil.formatNanos(nanos, this.serverSupportsFracSecs, true));
}
}
buf.append('\'');
setInternal(parameterIndex, buf.toString()); // SimpleDateFormat is not
// thread-safe
}
}
}
this.parameterTypes[parameterIndex - 1 + getParameterIndexOffset()] = Types.TIMESTAMP;
}
}
示例9: setTimestampInternal
import java.sql.Timestamp; //导入方法依赖的package包/类
/**
* Set a parameter to a java.sql.Timestamp value. The driver converts this
* to a SQL TIMESTAMP value when it sends it to the database.
*
* @param parameterIndex
* the first parameter is 1, the second is 2, ...
* @param x
* the parameter value
* @param tz
* the timezone to use
*
* @throws SQLException
* if a database-access error occurs.
*/
private void setTimestampInternal(int parameterIndex, Timestamp x, Calendar targetCalendar, TimeZone tz, boolean rollForward) throws SQLException {
if (x == null) {
setNull(parameterIndex, java.sql.Types.TIMESTAMP);
} else {
checkClosed();
if (!this.sendFractionalSeconds) {
x = TimeUtil.truncateFractionalSeconds(x);
}
if (!this.useLegacyDatetimeCode) {
newSetTimestampInternal(parameterIndex, x, targetCalendar);
} else {
Calendar sessionCalendar = this.connection.getUseJDBCCompliantTimezoneShift() ? this.connection.getUtcCalendar()
: getCalendarInstanceForSessionOrNew();
x = TimeUtil.changeTimezone(this.connection, sessionCalendar, targetCalendar, x, tz, this.connection.getServerTimezoneTZ(), rollForward);
if (this.connection.getUseSSPSCompatibleTimezoneShift()) {
doSSPSCompatibleTimezoneShift(parameterIndex, x, sessionCalendar);
} else {
synchronized (this) {
if (this.tsdf == null) {
this.tsdf = new SimpleDateFormat("''yyyy-MM-dd HH:mm:ss", Locale.US);
}
StringBuffer buf = new StringBuffer();
buf.append(this.tsdf.format(x));
if (this.serverSupportsFracSecs) {
int nanos = x.getNanos();
if (nanos != 0) {
buf.append('.');
buf.append(TimeUtil.formatNanos(nanos, this.serverSupportsFracSecs, true));
}
}
buf.append('\'');
setInternal(parameterIndex, buf.toString()); // SimpleDateFormat is not
// thread-safe
}
}
}
this.parameterTypes[parameterIndex - 1 + getParameterIndexOffset()] = Types.TIMESTAMP;
}
}
示例10: 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!");
}
示例11: compareTimestamps
import java.sql.Timestamp; //导入方法依赖的package包/类
/**
* 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 + ")");
}
}
示例12: 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;
}
}