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);
   }

Comments

Popular posts from this blog

Read Images from a xlsx file using Apache POI

Read Excel using Apache POI - Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException:

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'