Skip to content
Related Articles

Related Articles

Improve Article

LocalDateTime withNano() method in Java with Examples

  • Last Updated : 30 Nov, 2018

The 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 remains the same.

Syntax:

public LocalDateTime withNano(int nanoSeconds)

Parameter: This method accepts a single mandatory parameter nanoSeconds which specifies the nano-seconds to be set in the resultant LocalDateTime instance. The value of this nano-seconds can range from 0 to 999999999.

Returns: The function returns 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 remains the same.

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



Below programs illustrate the LocalDateTime.withNano() method:

Program 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.toString());
  
        // Get 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

Program 2:




// 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
                  .parse("2015-04-06T10:15:30");
  
        // Get the String representation of this LocalDateTime
        System.out.println("Original LocalDateTime: "
                           + dt.toString());
  
        // Get 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

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#withNano(int)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :