Java中的LocalDateTime类的withNano()方法用于获取此LocalDateTime的副本,并将纳秒更改为作为此方法的参数传递的纳秒。此LocalDateTime的其余值保持不变。
用法:
public LocalDateTime withNano(int nanoSeconds)
参数:此方法接受单个强制性参数nanoSeconds,该参数指定要在结果LocalDateTime实例中设置的纳秒。此纳秒的值的范围可以从0到999999999。
返回值:该函数返回LocalDateTime实例,该实例的纳秒更改为作为此方法的参数传递的纳秒。此LocalDateTime的其余值保持不变。
异常:如果纳秒值无效,该函数将引发DateTimeException。
以下示例程序旨在说明LocalDateTime.withNano()方法:
示例1:
// Program to illustrate the withNano() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
// Get the LocalDateTime instance
LocalDateTime dt = LocalDateTime.now();
// Get the String representation of this LocalDateTime
System.out.println("Original LocalDateTime: "
+ dt.toSt