Java中的 ZonedDateTime minusNanos() 方法及示例
ZonedDateTime类的minusNanos()方法,用于从此 ZonedDateTime 中减去纳秒数,并在减法后返回 ZonedDateTime 的副本。此方法适用于即时时间线,减去一纳秒返回早一纳秒的时间线。请注意,此方法与日、月和年使用的方法不同。此实例是不可变的,不受此方法调用的影响。
句法:
public ZonedDateTime minusNanos(long nanoseconds)
参数:此方法接受单个参数nanoseconds ,表示要减去的纳秒,它可以是负数。
返回值:此方法返回基于此日期时间减去纳秒的ZonedDateTime 。
异常:如果结果超出支持的日期范围,此方法将引发DateTimeException 。
下面的程序说明了 minusNanos() 方法:
方案一:
// Java program to demonstrate
// ZonedDateTime.minusNanos() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a ZonedDateTime object
ZonedDateTime zoneddatetime
= ZonedDateTime.parse(
"2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
// print instance
System.out.println("ZonedDateTime before"
+ " subtracting nanoseconds: "
+ zoneddatetime);
// subtract 30000000 nanoseconds
ZonedDateTime returnvalue
= zoneddatetime.minusNanos(30000000);
// print result
System.out.println("ZonedDateTime after "
+ " subtracting 30000000 nanoseconds: "
+ returnvalue);
}
}
ZonedDateTime before subtracting nanoseconds: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
ZonedDateTime after subtracting 30000000 nanoseconds: 2018-12-06T19:21:12.093+05:30[Asia/Calcutta]
方案二:
// Java program to demonstrate
// ZonedDateTime.minusNanos() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a ZonedDateTime object
ZonedDateTime zoneddatetime
= ZonedDateTime.parse(
"2018-10-25T23:12:31.123+02:00[Europe/Paris]");
// print instance
System.out.println("ZonedDateTime before"
+ " subtracting nanoseconds: "
+ zoneddatetime);
// subtract 52000000 nanoseconds
ZonedDateTime returnvalue
= zoneddatetime.minusNanos(52000000);
// print result
System.out.println("ZonedDateTime after "
+ " subtracting 52000000 nanoseconds: "
+ returnvalue);
}
}
ZonedDateTime before subtracting nanoseconds: 2018-10-25T23:12:31.123+02:00[Europe/Paris]
ZonedDateTime after subtracting 52000000 nanoseconds: 2018-10-25T23:12:31.071+02:00[Europe/Paris]
参考: https: Java/time/ZonedDateTime.html#minusNanos(long)