Java - Write code to add Year to Date


Write code to add Year 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(addYear(startDate, addnos));
  }//w  ww  . j a va  2  s.  c o  m

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

Related Exercise