×

How to add 6 Months to the Current Date in Java?

In this tutorial, we will learn how to add 6 months to the local or current date in Java language. We will begin our topic with basic concepts and would reach a java code to finally understand the proper way to accomplish this task.

To add months to the current date, there exists a method known as the plus months()method, which belongs to the java class called LocalDate class.

This method is encompassed in Java version 8throughDataTime API.

Users can easily increase the months as per their requirements.

To be able to use this class, the needs need to import a package known as java.time package.

Let us see some of the examples to understand the working of the method.

Examples:

  1. Input - 2021-05-05   output - 2020-11-05. [ After adding 6 months]
  2. Input - 2022-06-05   output - 2022-12-05.[ After adding 6 months]
  3. Input - 2023-05-05   output - 2023-11-05.[ After adding 6 months]
  4. Input - 2020-05-05   output - 2020-11-05. [ After adding 6 months]
  5. Input - 2015-03-09   output - 2015-08-09. [ After adding 6 months]

Let us first see the syntax of the method to be discussed here.

Syntax:

public LocalDateplusmonths (long monthsTobeAdded)

Description of syntax:

  • monthsTobeAdded– It is the only parameter of the method. It accepts the number of months which is to be added to the current date.
  • – It is the name of the method that would increase the number of months.
  • Return - A copy of the LocalDate is returned by the method plusMonths()  after the addition of the required months.
  • Exception - An exception known as DateTimeException is thrown in case the outcomeoverdoes the supported range of data.

Now, let us see the working of the above-stated method by using it in a java program.

Implementation 1:

import java.time.LocalDate;


//Example to Add 6 months to a current Date
public class Example1 {  
public static void main(String[] args){  

// considering a local date
LocalDate date1 = LocalDate.now();
// Displaying date
System.out.println("Current Date : "+date1);
// Adding 6 months to the date
LocalDate newDate1 = date1.plusMonths(6); 
System.out.println("Date after 6 months : "+newDate1);
}
}

Output:

How to Add 6 Months to the Current Date in Java

Explanation:  The current date is 28th November 2022 and the date after 6 months would be 28th May 2023. Here, LocalDate.now()is being used to get the current date.

With the help of the above method, it reads the current date and gives the output accordingly.

Implementation 2:

import java.time.LocalDate;


//Example to add  6 months to a Date
public class Example2 {  
public static void main(String[] args){  

// Considering any date
LocalDate date1 = LocalDate.parse("2022-07-08");
// Displaying the given date
System.out.println("Given Date : "+date1);
// Displaying the date after the addition of 6 months
LocalDate newDate1 = date1.plusMonths(6); 
System.out.println("New Date : "+newDate1);
}
}

Output:

How to Add 6 Months to the Current Date in Java

Explanation: In this code, we have considered the date as 8th July 2022, and after increasing it by six months the new date becomes 18 January 2023. Here, the date is being inputted by the user manually unlike implementation 1. LocalDate.parse() is being used to get the date from the user.

Implementation 3:

import java.time.LocalDate;


//Example to add 6 months to a Date
public class Example3 {  
public static void main(String[] args){  

// Considering any date
LocalDate date1 = LocalDate.parse("2027-08-18");
// Displaying the given date
System.out.println("Given Date : "+date1);
// Displaying the date after the addition of 6 months
LocalDate newDate1 = date1.plusMonths(6); 
System.out.println("New Date : "+newDate1);
}
}

Output:

How to Add 6 Months to the Current Date in Java

Explanation: In this code, we have considered the date as 2027-08-18, and after increasing it by six months the new date becomes 2028-02-18. Here LocalDate.parse() is being used to get the date from the user.

Summary:

We learned how to add six months to the current date in Java language. It was interesting to understand we can accomplish this task simply by taking a method called plus months () into account and providing 6 as the parameter.

However, we can increase any number of months by giving that number as a parameter in the method. We can also decrease the no. of months by giving a negative integer as a parameter to the plus months() method.


Related Topics

Zigzag Array in Java

In this tutorial, we discuss, what is zigzag array and its example. Even we will create the java program. In this program, we convert the simple array into a zigzag...

4 minutes read.

Java String subSequence() method

This method returns a new character sequence i.e. subsequence of current sequence Syntax: public CharSequence subSequence(int beginIndex, int endIndex) Parameter: beginIndex ? begin index, inclusive. endIndex ? end index, exclusive. Return: specified subsequnce Throws: It throws IndexOutOfBoundsException...

1 minute read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Program to Find the Common Elements between two Arrays in Java

In this article, we are going to learn how to find the common elements between two arrays using Java. Here, we use different approaches in Java to find the common...

3 minutes read.

Creating a Custom Generic Class in Java

To indicate parameter types when creating generic classes, we utilize the <> symbol. The syntax used to generate objects of a generic class is as follows. // To create an instance...

4 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Cosmic Superclass in Java

The parent class of all Java classes is the Object class. The Java Object class is the parent of all Java classes, whether directly or indirectly. The Object class is...

6 minutes read.

How to override toString() method in Java?

Java is an object-oriented language. It only works with classes and objects. Thus, whenever we need to calculate, we need an object or objects that belong to the class. The Java method...

2 minutes read.

Java Generate UUID

What java Generate UUID? A 128-bit long value that is universally unique serves as the basis for the terms UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier). Hexadecimal (octet) digits...

5 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Finding Odd Occurrence of a Number in Java

In this tutorial, we will learn how to find the odd occurrence of a number through a java program. Let us consider an array of non-negative integers. Here, except for one...

6 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Program to Find Square Root of a Number Without sqrt Method in Java

The Java Math class sqrt () function can be used to determine the square root of a number. In this section, we'll write a Java program to find a number's...

3 minutes read.

Java vs DotNET

Before understanding the differences between DotNET and Java, one must know about Java and DotNET. Java Java is a general-purpose programming language that is class-based and object-oriented, with minimal implementation dependencies. Regardless of...

9 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

Differences between Lock and Monitor in Java Concurrency

In this tutorial, we will discuss the overview of Lock and Monitor and the differences between them. Introduction Java Concurrency is the ability to perform specific tasks at a time parallelly. The...

4 minutes read.

Producer Consumer Problem in Java Using Synchronized Block

The producer-consumer dilemma is a well-known instance of a multi-process synchronization issue in computing. Two processes—the producer and the consumer—are described in the issue, and they share a single, fixed-size...

4 minutes read.

Java vs Dot Net

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.