Java Program to add year to current date using Calendar.add method

Java 8Object Oriented ProgrammingProgramming

Firstly, you need to import the following package for Calendar class in Java

import java.util.Calendar;

Create a Calendar object and display the current date and time

Calendar calendar = Calendar.getInstance();
System.out.println("Current Date and Time = " + calendar.getTime());

Now, let us add year using the calendar.add() method and Calendar.YEAR constant

calendar.add(Calendar.YEAR, 20);

The following is an example

Example

 Live Demo

import java.util.Calendar;
public class Demo {
   public static void main(String[] args) {
      Calendar calendar = Calendar.getInstance();
      System.out.println("Current Date = " + calendar.getTime());
      // Add 20 Years
      calendar.add(Calendar.YEAR, 20);
      System.out.println("Updated Date = " + calendar.getTime());
   }
}

Output

Current Date = Thu Nov 22 18:19:06 UTC 2018
Updated Date = Sun Nov 22 18:19:06 UTC 2038
raja
Published on 19-Dec-2018 15:22:54
Advertisements