getNanos()函數是Java SQL的Timestamp類的一部分,該函數用於獲取Timestamp對象的秒值的小數部分。該函數返回對象的nanos值。
函數簽名:
public int getNanos()
用法:
ts1.getNanos();
參數:該函數不接受任何參數。
返回值:該函數返回一個整數值,該整數值是對象的nanos值
異常:該函數不會引發任何異常
以下示例說明了getNanos()函數的使用
範例1:創建一個時間戳,並使用getNanos()獲取時間戳對象的小數部分。
// Java program to demonstrate the
// use of getNanos() function
import java.sql.*;
public class solution {
public static void main(String args[])
{
try {
// Create two timestamp objects
Timestamp ts = new Timestamp(10000);
// Display the timestamp object
System.out.println("Timestamp time:"
+ ts.toString());
// Set the value of the fractional part
// of timestamp object
// using setNanos function
ts.setNanos(1000000);
// Display the timestamp object's
// seconds' fractional part
System.out.println("New Timestamp time:"
+ ts.toString());
System.out.println(" Fractional Part:"
+ ts.getNanos());
}
catch (IllegalArgumentException e) {
// Display the error if any error has occured
System.err.println(e.getMessage());
}
}
}
輸出:
Timestamp time:1970-01-01 00:00:10.0 New Timestamp time:1970-01-01 00:00:10.001 Fractional Part:1000000
範例2:創建一個時間戳,並使用getNanos()獲取時間戳對象的小數部分,而不設置秒的任何小數值
// Java program to demonstrate the
// use of getNanos() function
import java.sql.*;
public class solution {
public static void main(String args[])
{
try {
// Create two timestamp objects
Timestamp ts = new Timestamp(10000);
// Display the timestamp object
System.out.println("Timestamp time:"
+ ts.toString());
// Display the timestamp object's
// seconds' fractional part
System.out.println("Fractional Part:"
+ ts.getNanos());
}
catch (IllegalArgumentException e) {
// Display the error if any error has occured
System.err.println(e.getMessage());
}
}
}
輸出:
Timestamp time:1970-01-01 00:00:10.0 Fractional Part:0
參考: https:// docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html
相關用法
- Java SQL Timestamp after()用法及代碼示例
- Java SQL Timestamp before()用法及代碼示例
- Java SQL Timestamp setNanos()用法及代碼示例
- Java SQL Timestamp getTime()用法及代碼示例
- Java SQL Timestamp setTime()用法及代碼示例
- Java UUID timestamp()用法及代碼示例
- Java Java.util.function.BiPredicate用法及代碼示例
- Java Java.util.function.IntPredicate用法及代碼示例
- Java Java.util.function.DoublePredicate用法及代碼示例
- Java Java.util.function.LongPredicate用法及代碼示例
- Java Function Interface用法及代碼示例
- Java Deflater getBytesWritten()用法及代碼示例
- Java Deflater getTotalOut()用法及代碼示例
- Java Deflater getAdler()用法及代碼示例
- Java ZipEntry getMethod()用法及代碼示例
注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 Java SQL Timestamp getNanos() function with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。