add hour in date java code example
Example 1: java Calendar add time
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -15);
calendar.add(Calendar.MONTH, 4);
Example 2: add one day to current timestamp date in java
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
public class AddTime {
public static void main( String[] args ) {
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println(timestamp);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timestamp.getTime());
cal.add(Calendar.SECOND, 30);
timestamp = new Timestamp(cal.getTime().getTime());
System.out.println(timestamp);
cal.setTimeInMillis(timestamp.getTime());
cal.add(Calendar.HOUR, 5);
timestamp = new Timestamp(cal.getTime().getTime());
System.out.println(timestamp);
cal.setTimeInMillis(timestamp.getTime());
cal.add(Calendar.DAY_OF_MONTH, 30);
timestamp = new Timestamp(cal.getTime().getTime());
System.out.println(timestamp);
cal.setTimeInMillis(timestamp.getTime());
cal.add(Calendar.MONTH, 6);
timestamp = new Timestamp(cal.getTime().getTime());
System.out.println(timestamp);
}
}