To subtract/minus seconds from the local date-time, Java provides a class i.e. LocalDateTime, and a method i.e. minusSeconds() method.
In this article, we are going to subtract seconds from local date-time with several running examples.
Let's see the code below.
/*
* Code example to minus seconds 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 seconds to the date
LocalDateTime newDate = date.minusSeconds(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 seconds are highlighted in black in the output
In the above code, 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 minusSeconds() method.
Now, let's have a look at this method signature:
public LocalDateTime minusSeconds(long seconds)
Package Name: java.time;
Class Name: LocalDateTime
Return Value: It returns a copy of LocalDateTime after subtracting the specified number of seconds, 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 want to subtract the seconds 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 minusSeconds() method. See the below code.
/*
* Code example to minus seconds 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 seconds to the date
LocalDateTime newDate = date.minusSeconds(2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T20:51:23.881671070
New Date : 2022-03-14T20:51:21.881671070
The minusSeconds() method accepts negative arguments as well.
If we pass the seconds as a negative value then it does the reversal operation and adds the seconds rather than subtracting. See the code below.
/*
* Code example to minus seconds 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);
// Adding 2 seconds to the date
LocalDateTime newDate = date.minusSeconds(-2);
// Display result
System.out.println("New Date : "+newDate);
}
}
Output:
Date : 2022-03-14T17:12:17
New Date : 2022-03-14T17:12:19