How to add years in current date in java - Java @ Desk

Thursday, May 16, 2013

How to add years in current date in java

In order to add years to current date or any date object, Calendar class is used. Need to create a new calendar object and the time is set to the date object. You need to use static property of Calendar class YEAR and add whatever months as per your requirement.

Click Get Current Date and Time across TimeZone in Java


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

public class AddYearsInDate {
    public static void main(String args[]) {
        Date currentDate = new Date();
        System.out.println("Current date is " + currentDate);
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(new Date());  
        calendar.add(Calendar.YEAR , 6);
        currentDate = calendar.getTime();
        System.out.println("New date after adding 6 years " + currentDate);
    }
}

Output is :
Current date is Thu May 16 15:52:44 IST 2013
New date after adding 6 years Thu May 16 15:52:44 IST 2019






No comments:

Post a Comment