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.
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.
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:
Current Date and Time : Fri Aug 03 11:48:38 UTC 2018 Modified Date and Time : Wed Oct 03 11:48:38 UTC 2018
Current Date and Time : Fri Aug 03 11:48:40 UTC 2018 Modified Date and Time : Tue Aug 03 11:48:40 UTC 2038
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
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.