LocalTime類的withNano()方法用於獲取此LocalTime的副本,並將nanos更改為傳遞給此方法的參數的nanos。此LocalTime的其餘值將保持不變。此實例是不可變的,不受此方法調用的影響。
用法:
public LocalTime withNano(int nano)
參數:此方法接受單個參數nano,該參數表示要在結果中設置的納秒(從0到999、999、999)。
返回值:此方法基於此時間返回帶有請求的nano的LocalTime實例。
異常:如果nano值無效,則此方法引發異常DateTimeException
以下示例程序旨在說明withNano()方法:
程序1:
// Java program to demonstrate
// LocalTime.withNano() 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 time
System.out.println("Old LocalTime: "
+ time);
// Get a new LocalDateTime with nanos 100000
LocalTime newtime = time.withNano(100000);
// print result
System.out.println("New LocalDateTime: "
+ newtime);
}
}
輸出:
Old LocalTime: 19:34:50.630 New LocalDateTime: 19:34:50.000100
程序2:
// Java program to demonstrate
// LocalTime.withNano() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalTime object
LocalTime time
= LocalTime.parse("01:21:30.13");
// print time
System.out.println("Old LocalTime: "
+ time);
// Get a new LocalDateTime with nanos 999999
LocalTime newtime = time.withNano(999999);
// print result
System.out.println("New LocalDateTime: "
+ newtime);
}
}
輸出:
Old LocalTime: 01:21:30.130 New LocalDateTime: 01:21:30.000999999
參考文獻: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#withNano(int)
相關用法
- Java LocalDateTime withNano()用法及代碼示例
- Java ZonedDateTime withNano()用法及代碼示例
- Java OffsetDateTime withNano()用法及代碼示例
- Java LocalTime with()用法及代碼示例
- Java LocalTime until()用法及代碼示例
- Java LocalTime from()用法及代碼示例
- Java LocalTime plus()用法及代碼示例
- Java LocalTime get()用法及代碼示例
- Java LocalTime getNano()用法及代碼示例
- Java LocalTime ofSecondOfDay()用法及代碼示例
- Java LocalTime getLong()用法及代碼示例
- Java LocalTime getSecond()用法及代碼示例
- Java LocalTime toEpochSecond()用法及代碼示例
- Java LocalTime ofNanoOfDay()用法及代碼示例
- Java LocalTime plusMinutes()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LocalTime withNano() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。