GregorianCalendar add() Method in Java - GeeksforGeeks

07 Aug, 2018
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:
// 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());    }}
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:
// Java Program to demonstrate add() methodimport 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());    }}
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:
// 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());    }}
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()
Java - util package
Java-Functions
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
Internal Working of HashMap in Java
Strings in Java