LocalDateTime withNano() method in Java with Examples Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like 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: Comment More infoAdvertise with us Next Article LocalDateTime withYear() method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java-LocalDateTime Java-Functions Java-time package Practice Tags : Java Similar Reads LocalTime withNano() method in Java with Examples The withNano() method of a LocalTime class is used to get a copy of this LocalTime with the nanos changed to the nanos passed as the parameter to this method. The remaining values of this LocalTime will remain the same. This instance is immutable and unaffected by this method call. Syntax: public Lo 2 min read LocalDateTime with() Method in Java with Examples In LocalDateTime class, there are two types of with() method depending upon the parameters passed to it. with(TemporalAdjuster adjuster) with(TemporalAdjuster adjuster) method of the LocalDateTime class used to adjusted this date-time using TemporalAdjuster and after adjustment returns the copy of a 3 min read LocalDateTime withHour() method in Java with Examples The withHour() method of LocalDateTime class in Java is used to get a copy of this LocalDateTime with the hours changed to the hours passed as the parameter to this method. The remaining values of this LocalDateTime remains the same. Syntax: public LocalDateTime withHour(int hours) Parameter: This m 2 min read LocalDateTime withYear() method in Java with Examples The withYear() method of LocalDateTime class in Java is used to get a copy of this LocalDateTime with the year changed to the year passed as the parameter to this method. The remaining values of this LocalDateTime remains the same. Syntax: public LocalDateTime withYear(int year) Parameter: This meth 2 min read LocalDateTime withMinute() method in Java with Examples The withMinute() method of LocalDateTime class in Java is used to get a copy of this LocalDateTime with the minutes changed to the minutes passed as the parameter to this method. The remaining values of this LocalDateTime remains the same. Syntax: public LocalDateTime withMinute(int minutes) Paramet 2 min read LocalDateTime withSecond() method in Java with Examples The withSecond() method of LocalDateTime class in Java is used to get a copy of this LocalDateTime with the seconds changed to the seconds passed as the parameter to this method. The remaining values of this LocalDateTime remains the same. Syntax: public LocalDateTime withSecond(int seconds) Paramet 2 min read Like