Home Tutorial Java Core Java date add months

 
 

Share on Google+Share on Google+
Java date add months
Posted on: October 9, 2012 at 12:00 AM
Advertisement
In this tutorial, you will learn how to add months to date.

Java date add months

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

Here, we are going to add few months 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 months to the calendar and using the Date class, we have got the date of that day.

Example:

import java.util.*;

public class AddMonthsToDate{

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

Output:

Today's Date: Tue Oct 09 13:08:07 IST 2012
Date after 4 months: Sat Feb 09 13:08:07 IST 2013
Advertisement

Related Tags for Java date add months:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 9, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com