Java - Write code to add Second to Date


Write code to add Second to Date


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

public class Main {
  public static void main(String[] argv) {
    Date startDate = new Date();
    int addnos = 42;
    System.out.println(addSecond(startDate, addnos));
  }/*from  ww w.j  a  v a  2s  . c o  m*/

  public static Date addSecond(Date startDate, int addnos) {
    Calendar cc = Calendar.getInstance();
    if (startDate != null) {
      cc.add(Calendar.SECOND, addnos);
      return cc.getTime();
    } else {
      return null;

Related Exercise