📜  Java中的 LocalTime minusNanos() 方法及示例

📅  最后修改于: 2022-05-13 01:55:41.865000             🧑  作者: Mango

Java中的 LocalTime minusNanos() 方法及示例

LocalTimeminusNanos()方法用于从此 LocalTime 中减去指定的纳秒值,并将结果作为 LocalTime 对象返回。这个瞬间是不变的。计算在午夜左右结束。

句法:

public LocalTime minusNanos(long nanosecondsToSubtract)

参数:此方法接受单个参数nanosecondsToSubtract ,它是要减去的纳秒值,它可以是负值。

返回值:此方法返回基于此时间减去纳秒的LocalTime

下面的程序说明了 minusNanos() 方法:

方案一:

// Java program to demonstrate
// LocalTime.minusNanos() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime object
        LocalTime time
            = LocalTime.parse("19:34:50.63");
  
        // print LocalTime
        System.out.println("LocalTime before subtraction: "
                           + time);
  
        // subtract 34000000 nanoseconds using minusNanos()
        LocalTime value = time.minusNanos(34000000);
  
        // print result
        System.out.println("LocalTime after subtraction: "
                           + value);
    }
}
输出:
LocalTime before subtraction: 19:34:50.630
LocalTime after subtraction: 19:34:50.596

方案二:

// Java program to demonstrate
// LocalTime.minusNanos() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime object
        LocalTime time
            = LocalTime.parse("01:00:01");
  
        // print LocalTime
        System.out.println("LocalTime before subtraction: "
                           + time);
  
        // subtract 971200000 nanoseconds  using minusNanos()
        LocalTime value = time.minusNanos(971200000);
  
        // print result
        System.out.println("LocalTime after subtraction: "
                           + value);
    }
}
输出:
LocalTime before subtraction: 01:00:01
LocalTime after subtraction: 01:00:00.028800

参考资料: https: Java/time/LocalTime.html#minusNanos(long)