GregorianCalendar add() Method in Java

The java.util.GregorianCalendar.add(int calendarfield, int time) is an in-built method of GregorianCalendar class in Java. The function accepts a calendar field and the amount of time to be added to that particular calendar field as a parameter. Based on the calendar rules, the method adds or deducts, based on its sign, the specified amount of time to the specified field.

Syntax:

public void add(int calendarfield, int time)

Parameters: The function accepts two mandatory parameters which are described below:



  • calendarfield: The calendar field which is to be modified.
  • time : The amount of time to be added.

Return Values: This method has no return value.

Exception: The method throws IllegalArgumentException if calendarfield has values ZONE_OFFSET, DST_OFFSET, or unknown, or if any of the calendar fields has an out-of-range value.

Examples:

Current Date and Time : Mon Jul 23 12:46:05 UTC 2018

Input : calendarfied = GregorianCalendar.YEAR, time = 2
Output : Thu Jul 23 12:46:05 UTC 2020

Input : calendarfied = GregorianCalendar.MONTH, time = 16
Output : Sat Nov 23 12:46:45 UTC 2019

Below programs illustrate the use of java.util.GregorianCalendar.add() method in Java:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate add() method
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Creating a new calendar
        GregorianCalendar newcalendar = (GregorianCalendar)
                           GregorianCalendar.getInstance();
  
        // Display the current date and time
        System.out.println(" Current Date and Time : "
                           + newcalendar.getTime());
        // Adding two months to the current date
        newcalendar.add((GregorianCalendar.MONTH), 2);
  
        // Display the modified date and time
        System.out.println(" Modified Date and Time : "
                           + newcalendar.getTime());
    }
}

chevron_right


Output:

Current Date and Time : Fri Aug 03 11:48:38 UTC 2018
 Modified Date and Time : Wed Oct 03 11:48:38 UTC 2018

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate add() method
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Creating a new calendar
        GregorianCalendar newcalendar = (GregorianCalendar)
                           GregorianCalendar.getInstance();
  
        // Display the current date and time
        System.out.println(" Current Date and Time : "
                           + newcalendar.getTime());
        // Adding twenty years to the current date
        newcalendar.add((GregorianCalendar.YEAR), 20);
        // Display the modified date and time
        System.out.println(" Modified Date and Time : "
                           + newcalendar.getTime());
    }
}

chevron_right


Output:

Current Date and Time : Fri Aug 03 11:48:40 UTC 2018
 Modified Date and Time : Tue Aug 03 11:48:40 UTC 2038

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate
// GregorianCalendar.add()
// function 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Creating a new calendar
        GregorianCalendar newcalendar = (GregorianCalendar)
                           GregorianCalendar.getInstance();
  
        // Display the current date and time
        System.out.println(" Current Date and Time : "
                           + newcalendar.getTime());
        // Deducting 16 months to the current date
        newcalendar.add((GregorianCalendar.MONTH), -16);
        // Display the modified date and time
        System.out.println(" Modified Date and Time : "
                           + newcalendar.getTime());
  
        // Deducting twelve years to the current date
        newcalendar.add((GregorianCalendar.MONTH), -12);
        // Display the modified date and time
        System.out.println(" Modified Date and Time : "
                           + newcalendar.getTime());
    }
}

chevron_right


Output:

Current Date and Time : Fri Aug 03 11:48:43 UTC 2018
 Modified Date and Time : Mon Apr 03 11:48:43 UTC 2017
 Modified Date and Time : Sun Apr 03 11:48:43 UTC 2016

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#add()



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.