Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Calendar clone() Method in Java with Examples
Next article icon

Calendar add() Method in Java with Examples

Last Updated : 02 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The add() method of Calendar class present inside is used to add or subtract from the given calendar field(int field), a specific amount of time(int amt), based on the calendar’s rules.

Syntax: 

public abstract void add(int field, int amt)

Parameters: The method takes two parameters: 

  • The field of the calendar on which the operation is to be performed. (Integer type)
  • The amount of time needed to be subtracted. (Integer type)

Return Value: The method does not return any value.

Example 1:

Java




// Java Program to Illustrate add() Method
// of Calendar class
 
// Importing required classes
import java.util.Calendar;
 
// Main class
public class CalendarClassDemo {
 
    // Main driver method
    public static void main(String args[])
    {
        // Creating a Calendar class object
        Calendar calndr = Calendar.getInstance();
 
        // Displaying the current date
        // using getTime() method
        System.out.println("Current Date: "
                           + calndr.getTime());
 
        // Adding 50 days to the
        // Current Calendar
        // using add() method
        calndr.add(Calendar.DATE, 50);
 
        // Displaying the date now using getTime() method
        System.out.println("After 50 days: "
                           + calndr.getTime());
 
        // Subtracting 6 months from
        // the Current calendar
        calndr.add(Calendar.MONTH, -6);
 
        // Displaying the date now using getTime() method
        System.out.println("6 months ago it was: "
                           + calndr.getTime());
 
        // Subtracting 2 year from
        // the Current calendar
        calndr.add(Calendar.YEAR, -2);
 
        // Displaying the date now using getTime() method
        System.out.println("2 years ago it was: "
                           + calndr.getTime());
    }
}
 
 
Output: 
Current Date: Tue Feb 12 10:54:21 UTC 2019 After 50 days: Wed Apr 03 10:54:21 UTC 2019 6 months ago it was: Wed Oct 03 10:54:21 UTC 2018 2 years ago it was: Mon Oct 03 10:54:21 UTC 2016

 

Example 2:

Java




// Java Program to Illustrate add() Method
// of Calendar class
 
// Importing required classes
import java.util.Calendar;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar calndr = Calendar.getInstance();
 
        // Displaying the current date
        // using getTime() method
        System.out.println("Current Date: "
                           + calndr.getTime());
 
        // Adding 30 days to the current calendar
        // using add() method
        calndr.add(Calendar.DATE, 30);
 
        // Printing the corresponding date
        System.out.println("After 30 days: "
                           + calndr.getTime());
 
        // Subtracting 3 months from the current calendar
        calndr.add(Calendar.MONTH, -3);
 
        // Printing the corresponding date
        System.out.println("3 months ago it was: "
                           + calndr.getTime());
 
        // Subtracting 10 years from
        // the Current calendar
        calndr.add(Calendar.YEAR, -10);
 
        // Printing the corresponding date
        System.out.println("10 years ago it was: "
                           + calndr.getTime());
    }
}
 
 
Output: 
Current Date: Tue Feb 12 10:54:24 UTC 2019 After 30 days: Thu Mar 14 10:54:24 UTC 2019 3 months ago it was: Fri Dec 14 10:54:24 UTC 2018 10 years ago it was: Sun Dec 14 10:54:24 UTC 2008

 



Next Article
Calendar clone() Method in Java with Examples
author
chinmoy lenka
Improve
Article Tags :
  • Java
  • Java - util package
  • Java-Calendar
  • Java-Functions
Practice Tags :
  • Java

Similar Reads

  • Calendar get() method in Java with Examples
    The get(int field_value) method of Calendar class is used to return the value of the given calendar field in the parameter. Syntax: public int get(int field) Parameters: The method takes one parameter field_value of integer type and refers to the calendar whose value is needed to be returned. Return
    2 min read
  • Calendar set() Method in Java with Examples
    The set(int calndr_field, int new_val) method in Calendar class is used to set the calndr_field value to a new_val. The older field of this calendar get replaced by a new field. Syntax: public void set(int calndr_field, int new_val) Parameters: The method takes two parameters: calndr_field: This is
    2 min read
  • Calendar clear() Method in Java with Examples
    The clear() method in Calendar class is used to set all the calendar field values and the time value of this Calendar undefined. Note: Implementation of calendar class may use default field values for date and time calculations. Syntax: public final void clear() Parameters: The method does not take
    2 min read
  • Calendar isSet() Method in Java with Examples
    The isSet(int calndr_field) method in Calendar class is used to check whether the given calendar field has a value set or not. All the Cases for which the values have been set by the calculations of the internal field are triggered by a get method call. Syntax: public final boolean isSet(int calndr_
    2 min read
  • Calendar clone() Method in Java with Examples
    The clear() method in Calendar class is used to clone a calendar object. It basically creates a shallow copy of this object. Syntax: public Object clone() Parameters: The method does not take any parameters. Return Value: The method does not return any value. Below programs illustrate the working of
    2 min read
  • Calendar setTime() Method in Java with Examples
    The setTime(Date dt) method in Calendar class is used to set Calendars time represented by this Calendar's time value, with the given or passed date as a parameter. Syntax: public final void setTime(Date dt)) Parameters: The method takes one parameter dt of Date type and refers to the given date tha
    2 min read
  • Calendar complete() Method in Java with Examples
    The complete() method in Calendar class is used to fill any unset fields of the calendar fields. It follows the following process of completion: At first, if the time value has not been calculated, the computeTime() method is called to calculate it from calendar field values. Second, to calculate al
    2 min read
  • Calendar toString() Method in Java with Examples
    The toString() method in Calendar class is used to get the string representation of the Calendar object. This method in Calendar Class is just for debug process and not to be used as an operation. Syntax: public String toString() Parameters: The method does not take any parameters. Return Value: The
    1 min read
  • Calendar hashCode() Method in Java with Examples
    The hashCode() method in Calendar class is used to return the hash code for this Calendar Object.Syntax: public int hashCode() Parameters: The method does not take any parameters.Return Value: The method returns the hash code value for this calendar object..Below programs illustrate the working of h
    2 min read
  • Date clone() method in Java with Examples
    The clone() method of Date class in Java returns the duplicate of the passed Date object. This duplicate is just a shallow copy of the given Date object.Syntax: public Object clone() Parameters: The method does not accept any parameters.Return Value: The method returns a clone of the object.Below pr
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences