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

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

Upload csv file at Google Cloud Storage using java

Read Images from a xlsx file using Apache POI