Open In App

LocalDateTime withNano() method in Java with Examples

Last Updated : 29 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

withNano() method of LocalDateTime class in Java is used to get a copy of this LocalDateTime with the nano-seconds changed to the nano-seconds passed as the parameter to this method. The remaining values of this LocalDateTime remain the same.

Syntax: 

public LocalDateTime withNano(int nanoSeconds)

Parameter: Single mandatory parameter nanoSeconds which specify the nano-seconds to be set in the resultant LocalDateTime instance. The value of these nano-seconds can range from 0 to 999999999.

Return Type: A LocalDateTime instance with the nano-seconds changed to the nano-seconds passed as the parameter to this method. The remaining values of this LocalDateTime remain the same.

Exceptions: The function throws a DateTimeException if the nano-seconds value is invalid.

Example 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));
    }
}


Output: 

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

 

Example 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));
    }
}


Output: 

Original LocalDateTime: 2015-04-06T10:15:30
New LocalDateTime: 2015-04-06T10:15:30.000099999

 

Now we will be handling with care withNano(0) method is used for removing the nano seconds and retaining date-time till seconds.

Example 3:

Note: Special Case: If the seconds value is:00, then while doing at toString() will eliminate the seconds portion.

Example:

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));
        }
    }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads