Java - Write code to add Hour to Date


Write code to add Hour to Date


import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    Date startDate = new Date();
    int addnos = 42;
    System.out.println(addHour(startDate, addnos));
  }//w ww  .  ja v  a2 s . co  m

  public static Date addHour(Date startDate, int addnos) {
    Calendar cc = Calendar.getInstance();
    if (startDate != null) {
      cc.add(Calendar.HOUR, addnos);
      return cc.getTime();
    } else {
      return null;

Related Exercise