Java Second Add addSeconds(Date date, int seconds)

Here you can find the source of addSeconds(Date date, int seconds)

Description

Creates a new date as a clone of the specified date and adds the specified number of seconds to it.

License

MIT License

Parameter

Parameter Description
date a date.
seconds a number of seconds to add.

Return

the new date.

Declaration

public static Date addSeconds(Date date, int seconds) 

Method Source Code

//package com.java2s;
/*//w ww  . jav  a  2  s. c om
 * Copyright (c) 2015-2016 QuartzDesk.com.
 * Licensed under the MIT license (https://opensource.org/licenses/MIT).
 */

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * Creates a new date as a clone of the specified date and adds the specified number of
     * seconds to it.
     *
     * @param date    a date.
     * @param seconds a number of seconds to add.
     * @return the new date.
     */
    public static Date addSeconds(Date date, int seconds) {
        Date newDate = (Date) date.clone();

        Calendar cal = Calendar.getInstance();
        cal.setLenient(true);
        cal.setTime(newDate);
        cal.add(Calendar.SECOND, seconds);

        return cal.getTime();
    }
}

Related

  1. addSecond(Date date, int numberOfSecond)
  2. addSecond(Date date, int second)
  3. addSecond(Date source, int s)
  4. addSecond(int second)
  5. addSeconds(Date date, int seconds)
  6. addSeconds(final Date date, final int seconds)
  7. addSeconds(int addSeconds)