Javaexercise.com

How To Add Seconds To Date Or Current Date In Java?

To add seconds to the date-time in Java, you can use the plusSeconds() method of the LocalDateTime class of Java 8.

In this article, we are explaining to add seconds to local date-time in Java.

LocalDatetime is a class in java.time package that represents local date and time without a timezone. It was added to Java 1.8 version.

We can add seconds such as 1 second, 5 seconds, 10 seconds, 30 seconds, etc to date-time and the current date-time as well.

Adding Seconds to current Local Date-Time in Java

Here, we used the built-in plusSeconds() method of the LocalDateTime class to add seconds to the current date-time. We used the now() method to get the current date-time in Java.

/* 
 *  Code example to add seconds to 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);
		
		// Add 2 months to the date
		LocalDateTime newDate = date.plusSeconds(1); // let's add one second 
		
		// Display result
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2022-03-14T17:12:17
New Date : 2022-03-14T17:12:18
 

Now,  let's have a look at this method signature:

public LocalDateTime plusSeconds(long seconds)

Package Name: java.time;

Class Name: LocalDateTime

Return Value: It returns a copy of this LocalDateTime after adding the specified number of seconds.

Parameters: It takes a single parameter.

Exceptions: It throws DateTimeException.

Since: Java 1.8

Adding Seconds to LocalDateTime in Java

Here, we are adding seconds to string date after parsing to localdatetime object and then using the plusSeconds() method. Here, we added 1 second to the current date-time in Java.

/* 
 *  Code example to add seconds to 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 date into date time
	    LocalDateTime date = LocalDateTime.parse(strDate);
	    
		// Displaying date and time
		System.out.println("Date : "+date);
		
		// Add 1 second to the date
		LocalDateTime newDate = date.plusSeconds(1); // Adding one second 
		
		// Display result
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2022-03-14T17:12:17
New Date : 2022-03-14T17:12:18
 

Subtract Seconds from the date-time in Java

If we pass a negative value to the plusMinutes() method then it actually subtracts the seconds rather than adding. See the code to understand.

We can do this for subtracting too by passing the negative arguments. We subtracted 1 seconds from the date-time in Java.

/* 
 *  Code example to add seconds to 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 date into date time
	    LocalDateTime date = LocalDateTime.parse(strDate);
	    
		// Displaying date and time
		System.out.println("Date : "+date);
		
		// Add 1 second to the date
		LocalDateTime newDate = date.plusSeconds(-1); // let's add one second 
		
		// Display result
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2022-03-14T17:12:17
New Date : 2022-03-14T17:12:16