Date and Time

How to add Seconds to Date Java using LocalDateTime

Table of Contents

Program

import java.time.LocalDateTime;
import java.text.ParseException;
public class Seconds
{
    public static void main(String[] args) throws ParseException 
    {
        LocalDateTime dt1 = LocalDateTime.parse("2018-01-11T10:15:30");
        LocalDateTime dt2=dt1.plusSeconds(120);
        System.out.println("After 120 seconds added: "+dt2);
    }
}

Output

After 120 seconds added: 2018-01-11T10:17:30

Description

public LocalDateTime plusSeconds​(long seconds)

Returns a copy of this LocalDateTime with the specified number of seconds added.
This instance is immutable and unaffected by this method call.

Parameters:

seconds – the seconds to add, may be negative

Returns:

a LocalDateTime based on this date-time with the seconds added, not null

Throws:

DateTimeException – if the result exceeds the supported date range