LocalDateTime類的plusNanos()方法用於返回此日期時間的副本,其中添加了指定的nanoSeconds。
用法:
public LocalDateTime plusNanos(long nanoSeconds)
參數:它接受單個參數nanoSeconds,該參數指定要添加的nanoSeconds,該參數可能為負數。
返回值:此方法基於添加了nanoSeconds的日期時間返回LocalDateTime。
異常:程序將拋出DateTimeException,如果結果超出了受支持的nanoSeconds範圍,則將引發該異常。
以下程序說明了Java中的YearMonth.plusNanos()方法:
程序1:
// Program to illustrate the plusNanos() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
LocalDateTime dt1
= LocalDateTime
.parse("2018-01-11T10:15:30");
System.out.println("LocalDateTime with 10000 nanoSeconds added: "
+ dt1.plusNanos(10000));
}
}
輸出:
LocalDateTime with 10000 nanoSeconds added: 2018-01-11T10:15:30.000010
程序2:
// Program to illustrate the plusNanos() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
LocalDateTime dt1
= LocalDateTime
.parse("2018-01-11T08:15:30");
System.out.println("LocalDateTime with -1500 nanoSeconds added: "
+ dt1.plusNanos(-1500));
}
}
輸出:
LocalDateTime with -1500 nanoSeconds added: 2018-01-11T08:15:29.999998500
參考: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#plusNanos(long)
相關用法
- Java LocalTime plusNanos()用法及代碼示例
- Java Instant plusNanos()用法及代碼示例
- Java OffsetTime plusNanos()用法及代碼示例
- Java OffsetDateTime plusNanos()用法及代碼示例
- Java ZonedDateTime plusNanos()用法及代碼示例
- Java Duration plusNanos(long)用法及代碼示例
- Java LocalDateTime with()用法及代碼示例
- Java LocalDateTime plus()用法及代碼示例
- Java LocalDateTime from()用法及代碼示例
- Java LocalDateTime now()用法及代碼示例
- Java LocalDateTime until()用法及代碼示例
- Java LocalDateTime plusWeeks()用法及代碼示例
- Java LocalDateTime plusDays()用法及代碼示例
- Java LocalDateTime equals()用法及代碼示例
- Java LocalDateTime plusHours()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 LocalDateTime plusNanos() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。