Add 3 Months to the Calendar in Java



In this article, we will learn to add 3 months to the calendar. We will take the current date and add 3 months to it using Calendar class in Java. You'll see how to handle date manipulation and update the date accordingly.

Problem Statement

Write a program in Java to add 3 months to the calendar. Below is the demonstration of the same ?

Input

Current Date = Fri Nov 23 06:38:56 UTC 2018

Output

Updated Date = Sat Feb 23 06:38:56 UTC 2019

Different approaches

Below are the different approaches to add 3 months to the calendar ?

Using Calendar Class

Following are the steps to add 3 months to the calendar ?

Example

The following is an example of adding 3 months to the calendar ?

import java.util.Calendar;
public class Demo {
   public static void main(String[] args) {
       Calendar calendar = Calendar.getInstance();
       System.out.println("Current Date = " + calendar.getTime());
      // Add 3 months to the Calendar
      calendar.add(Calendar.MONTH, 3);
      System.out.println("Updated Date = " + calendar.getTime());
   }
}

Output

Current Date = Fri Nov 23 06:38:56 UTC 2018
Updated Date = Sat Feb 23 06:38:56 UTC 2019

Using Calendar and LocalDate class

Following are the steps to add 3 months to the calendar ?

Example

import java.time.LocalDate;
public class Demo {
   public static void main(String[] args) {
       LocalDate currentDate = LocalDate.now();
       System.out.println("Current Date = " + currentDate);
       // Add 3 months to the LocalDate
       LocalDate updatedDate = currentDate.plusMonths(3);
       System.out.println("Updated Date = " + updatedDate);
   }
}

Output

Current Date = 2024-09-16
Updated Date = 2024-12-16
Updated on: 2024-09-16T23:25:26+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements