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 LocalTime withNano(int nano)
Parameters: This method accepts a single parameter nano which represents the nano-of-second to set in the result, from 0 to 999, 999, 999.
Return value: This method returns a LocalTime instance based on this time with the requested nano.
Exception: This method throws a exception DateTimeException if the nano value is invalid
Below programs illustrate the withNano() method:
Program 1:
// Java program to demonstrate // LocalTime.withNano() method import java.time.*; public class GFG { public static void main(String[] args) { // create a LocalTime object LocalTime time = LocalTime.parse("19:34:50.63"); // print time System.out.println("Old LocalTime: " + time); // Get a new LocalDateTime with nanos 100000 LocalTime newtime = time.withNano(100000); // print result System.out.println("New LocalDateTime: " + newtime); } } |
Old LocalTime: 19:34:50.630 New LocalDateTime: 19:34:50.000100
Program 2:
// Java program to demonstrate // LocalTime.withNano() method import java.time.*; public class GFG { public static void main(String[] args) { // create a LocalTime object LocalTime time = LocalTime.parse("01:21:30.13"); // print time System.out.println("Old LocalTime: " + time); // Get a new LocalDateTime with nanos 999999 LocalTime newtime = time.withNano(999999); // print result System.out.println("New LocalDateTime: " + newtime); } } |
Old LocalTime: 01:21:30.130 New LocalDateTime: 01:21:30.000999999
References: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#withNano(int)
Recommended Posts:
- OffsetDateTime withNano() method in Java with examples
- ZonedDateTime withNano() method in Java with Examples
- LocalDateTime withNano() method in Java with Examples
- LocalTime with() Method in Java with Examples
- LocalTime from() method in Java with Examples
- LocalTime plus() method in Java with Examples
- LocalTime until() Method in Java with Examples
- LocalTime get() method in Java with Examples
- LocalTime atOffset() method in Java with Examples
- LocalTime truncatedTo() method in Java with Examples
- LocalTime ofInstant() method in Java with Examples
- LocalTime isSupported() method in Java with Examples
- LocalTime getSecond() method in Java with Examples
- LocalTime atDate() method in Java with Examples
- LocalTime ofNanoOfDay() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



