×

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

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal: Using toOcatlString() method Using user-defined logic Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer...

2 minutes read.

Read the XLSX file in Java

Excel files contain cells; reading an excel file in Java differs from reading a word file. JDK doesn't have a direct API that can read or write Word or Excel...

3 minutes read.

Thread Concept in Java

What is a Thread? A subprocess that is lightweight in Java is known as a thread. It is the smallest unit of a process. For the running of multiple tasks parallelly...

3 minutes read.

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram. Some examples of pangram...

3 minutes read.

Sleep Time in Java

Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a...

4 minutes read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

Various operations on the Queue using Stack in Java

The Java Collections Framework's core data structures are the Stack and Queue. They are used to store and retrieve identical data in a presentation sequence. These two linear data structures...

7 minutes read.

Java Integer reverse() method

The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation. Syntax public static int reverse (int i)  Parameters The parameter...

1 minute read.

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 minutes read.

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

How to Convert Decimal to Binary in Java

How to Convert Decimal to Binary in Java There are two methods to convert Decimal to Binary. Using toBinaryString() method Using user-defined logic Using Integer.toBinaryString() The toBinaryString() is a static method of Integer...

2 minutes read.

Sierpinski Number in Java

The Sierpinski triangle—is it a fractal? The Sierpinski Triangle fractals. A self-similar fractal is the Sierpinski triangle. It is made of an equilateral triangle with its residual area successively reduced by...

3 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Program to Reverse a Number in Java

In order to reverse a number, the digit in the first place must be swapped with the digit in the final position, the second digit with the second-to-last digit, and...

3 minutes read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.

Java String toUpperCase() methods

Java String toUpperCase() method is used to convert all the characters of the String into upper case. Syntax public String toUpperCase() public String toUpperCase(Locale locale) Returns It returns upper case String. Java String toUpperCase() Example 1: public...

1 minute read.

Java float vs double

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...

7 minutes read.

How to Set Environment Variables for Java

Introduction Java is an object-oriented programming language that is based on classes and can be employed mostly to develop web and desktop applications. No matter the computer architecture, Java applications are...

7 minutes read.

Java String isEmpty() method

Java String isEmpty() method checks whether current String is empty or not. Syntax: public boolean isEmpty() Returns It returns true, if length of String is 0 otherwise false. Java String isEmpty() method example 1    ...

1 minute read.