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
Post a Comment