Change timestamp by adding seconds in Java


1) Using Timestamp 
                long myDate= System.currentTimeMillis();
       int sec = 300;
       Timestamp originalTime = new Timestamp(myDate);
       Calendar calender = Calendar.getInstance();
        calender.setTimeInMillis(originalTime.getTime());
        calender.add(Calendar.SECOND, sec);
        Timestamp afterAdd = new Timestamp(calender.getTime().getTime());

       System.out.println(originalTime);
       System.out.println(afterAdd);

2) Using given date
          public void addTimeBySecondsDemo(Date date,int sec){
        //int sec = 300;
        System.out.println("Given date:"+date);
        Calendar calender = Calendar.getInstance();
        calender.setTimeInMillis(date.getTime());
        calender.add(Calendar.SECOND, sec);
        Date changeDate=cal.getTime();
        System.out.println("changeDate ..:"+changeDate);
   }

Popular posts from this blog

Can not connect to git using ssh (Permissions 0644 for '/root/.ssh/id_rsa' are too open.

Upload file on Google cloud storage using Java servlet on google app engine

Session timeout interceptor in struts 2