当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java SQL Timestamp getNanos()用法及代码示例


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



相关用法


注:本文由纯净天空筛选整理自andrew1234大神的英文原创作品 Java SQL Timestamp getNanos() function with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。