Home Tutorial Java Core Java date add year

 
 

Java date add year
Posted on: October 9, 2012 at 12:00 AM
In this tutorial, you will learn how to add years to date.

Java date add year

In this tutorial, you will learn how to add years to date.

Here, we are going to add few years to current date and return the date of that day. For this, we have created a calendar instance and get a date to represent the current date. Then using the method add() of Calendar class, we have added 4 years to the calendar and using the Date class, we have got the date of that day.

Example

import java.util.*;

public class AddYearsToDate{

  public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    Date today = calendar.getTime();
    System.out.println("Today's Date: " + today);
    calendar.add(Calendar.YEAR, 4);
    Date addYears = calendar.getTime();
    System.out.println("Date after 4 years: " + addYears);
  }
}

Output

Today's Date: Tue Oct 09 17:02:24 IST 2012
Date after 4 years: Sun Oct 09 17:02:24 IST 2016

Related Tags for Java date add year:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.