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 : calendarfield = GregorianCalendar.YEAR, time = 2
Output : Thu Jul 23 12:46:05 UTC 2020
Input : calendarfield = 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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
GregorianCalendar newcalendar = (GregorianCalendar)
GregorianCalendar.getInstance();
System.out.println(" Current Date and Time : "
+ newcalendar.getTime());
newcalendar.add((GregorianCalendar.MONTH), 2 );
System.out.println(" Modified Date and Time : "
+ newcalendar.getTime());
}
}
|
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:
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
GregorianCalendar newcalendar = (GregorianCalendar)
GregorianCalendar.getInstance();
System.out.println(" Current Date and Time : "
+ newcalendar.getTime());
newcalendar.add((GregorianCalendar.YEAR), 20 );
System.out.println(" Modified Date and Time : "
+ newcalendar.getTime());
}
}
|
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:
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
GregorianCalendar newcalendar = (GregorianCalendar)
GregorianCalendar.getInstance();
System.out.println(" Current Date and Time : "
+ newcalendar.getTime());
newcalendar.add((GregorianCalendar.MONTH), - 16 );
System.out.println(" Modified Date and Time : "
+ newcalendar.getTime());
newcalendar.add((GregorianCalendar.MONTH), - 12 );
System.out.println(" Modified Date and Time : "
+ newcalendar.getTime());
}
}
|
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()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Nov, 2022
Like Article
Save Article