📌  相关文章
📜  Java中的 LocalDateTime withNano() 方法及示例

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

Java中的 LocalDateTime withNano() 方法及示例

Java中 LocalDateTime 类的withNano()方法用于获取此 LocalDateTime 的副本,其中纳秒更改为作为参数传递给此方法的纳秒。此 LocalDateTime 的其余值保持不变。

句法:

public LocalDateTime withNano(int nanoSeconds)

参数:单个强制参数nanoSeconds ,它指定要在结果 LocalDateTime 实例中设置的纳秒。这些纳秒的值可以在 0 到 999999999 之间。

返回类型:一个LocalDateTime 实例,将纳秒更改为作为参数传递给此方法的纳秒。此 LocalDateTime 的其余值保持不变。

异常:如果纳秒值无效,该函数将引发DateTimeException

示例1:

Java
// Java Program to illustrate withNano() method of
// LocalDateTime class
 
// Importing required classes
import java.time.*;
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating tan instance of LocalDateTime class
        LocalDateTime dt = LocalDateTime.now();
 
        // Getting the String representation of this
        // LocalDateTime
        System.out.println("Original LocalDateTime: "
                           + dt.toString());
 
        // Getting a new LocalDateTime with nano-seconds 0
        System.out.println("New LocalDateTime: "
                           + dt.withNano(0));
    }
}


Java
// Java Program to illustrate withNano() method of
// LocalDateTime class
 
// Importing required classes
import java.time.*;
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of LocalDateTime class inside
        // main()
        LocalDateTime dt
            = LocalDateTime.parse("2015-04-06T10:15:30");
 
        // Getting the String representation of this
        // LocalDateTime
        System.out.println("Original LocalDateTime: "
                           + dt.toString());
 
        // Getting a new LocalDateTime with nano-seconds
        // 99999
        System.out.println("New LocalDateTime: "
                           + dt.withNano(99999));
    }
}


Java
// Java Program to illustrate withNano() method of
// LocalDateTime class
 
// Importing required classes
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws InterruptedException
    {
 
        // Creating an object of DateTimeFormatter class
        DateTimeFormatter dtf
            = DateTimeFormatter.ISO_DATE_TIME;
 
        // If condition holds true
        while (true) {
 
            // Making thread to sleep for 500 nanoseconds
            Thread.sleep(500);
 
            // Print and display commands
            System.out.println("Original :00 seconds --> "
                               + LocalDateTime.now());
            System.out.println(
                "Without Formatter(Observe when seconds is :00) --> "
                + LocalDateTime.now().withNano(0));
            System.out.println(
                "With Formatter -> "
                + LocalDateTime.now().withNano(0).format(
                    dtf));
        }
    }
}


输出:
Original LocalDateTime: 2018-11-30T12:54:17.484
New LocalDateTime: 2018-11-30T12:54:17

示例 2:

Java

// Java Program to illustrate withNano() method of
// LocalDateTime class
 
// Importing required classes
import java.time.*;
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of LocalDateTime class inside
        // main()
        LocalDateTime dt
            = LocalDateTime.parse("2015-04-06T10:15:30");
 
        // Getting the String representation of this
        // LocalDateTime
        System.out.println("Original LocalDateTime: "
                           + dt.toString());
 
        // Getting a new LocalDateTime with nano-seconds
        // 99999
        System.out.println("New LocalDateTime: "
                           + dt.withNano(99999));
    }
}
输出:
Original LocalDateTime: 2015-04-06T10:15:30
New LocalDateTime: 2015-04-06T10:15:30.000099999

示例 3:

例子:

Java

// Java Program to illustrate withNano() method of
// LocalDateTime class
 
// Importing required classes
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws InterruptedException
    {
 
        // Creating an object of DateTimeFormatter class
        DateTimeFormatter dtf
            = DateTimeFormatter.ISO_DATE_TIME;
 
        // If condition holds true
        while (true) {
 
            // Making thread to sleep for 500 nanoseconds
            Thread.sleep(500);
 
            // Print and display commands
            System.out.println("Original :00 seconds --> "
                               + LocalDateTime.now());
            System.out.println(
                "Without Formatter(Observe when seconds is :00) --> "
                + LocalDateTime.now().withNano(0));
            System.out.println(
                "With Formatter -> "
                + LocalDateTime.now().withNano(0).format(
                    dtf));
        }
    }
}

输出: