To subtract/minus nanoseconds from the local date-time, Java provides a class i.e. LocalDateTime, and a method i.e. minusNanos() method.
In this article, we are going to subtract nanoseconds from local date-time with several running examples.
Let's see the code below.
/*
* Code example to minus nenoseconds from date in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// String date is given
String strDate = "2022-03-14T17:12:17";
// parse the string date into date time
LocalDateTime date = LocalDateTime.parse(strDate);
// Displaying date and time
System.out.println("Date : "+date);
// Subtract 2 nenoseconds to the date
LocalDateTime newDate = date.minusNanos(2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T17:12:17
New Date : 2022-03-14T17:12:15
The subtracted nanoseconds are highlighted in black in the output
In the code above, we first get parsed the String date to the LocalDateTime object by using the parse() method.
If you already have locadatetime object, then you don't need to parse it.
You can directly call the minusNanos() method.
Now, let's have a look at this method signature:
public LocalDateTime minusNanos(long nanos)
Package Name: java.time;
Class Name: LocalDateTime
Return Value: It returns a copy of localdatetime after subtracting the specified number of nanoseconds, not null.
Parameters: It takes a single long type value. It may be negative.
Exceptions: It throws a DateTimeException if the result exceeds the supported(either MIN or MAX) date range.
Version: Since 1.8
Let's see some more examples.
If you wish to subtract the nanoseconds from the current local date-time then use the below code.
Here, we used the now() method to get the current date-time and then used the minusNanos() method. See the below code.
/*
* Code example to minus nenoseconds from date in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// Take current date and time
LocalDateTime date = LocalDateTime.now();
// Displaying date and time
System.out.println("Date : "+date);
// Subtract 2 nenoseconds to the date
LocalDateTime newDate = date.minusNanos(2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T20:58:31.638082589
New Date : 2022-03-14T20:58:31.638082587
The minusNanos() method accepts negative arguments as well.
If we pass the nanoseconds as negative values then it does the reversal operation and adds the nanoseconds rather than subtracting. See the code below.
/*
* Code example to minus nenoseconds from date in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// String date is given
String strDate = "2022-03-14T17:12:17";
// parse the string date into date time
LocalDateTime date = LocalDateTime.parse(strDate);
// Displaying date and time
System.out.println("Date : "+date);
// Add 2 nenoseconds to the date
LocalDateTime newDate = date.minusNanos(-2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T17:12:17
New Date : 2022-03-14T17:12:17.000000002