×

How to add 4 Years to the Current Date in Java?

In this tutorial, we will learn how to add 4 years 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 years to the current date, there exists a method known as the Calender.add()method, which belongs to the java class called Calender 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.Calendar package.

To accomplish this task, we have a special method called Calender.add () method in java.

Syntax:

Current_date.add(Calender.YEAR, years)

Understanding the above syntax

  • It is a variable to store the current or today’s date.
  • it is a method that adds the required no. of years to the current date.
  • It fetches the current year.
  • It refers to the number of years that is to be added to the current date.

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

Examples:

  1. Input - 2021-05-05   output - 2025-11-05. [ After adding 4 years]
  2. Input - 2022-06-05   output - 2026-12-05. [ After adding4 years]
  3. Input - 2023-05-05   output - 2027-11-05. [ After adding 4 years]
  4. Input - 2020-05-05   output - 2024-11-05. [ After adding 4 years]
  5. Input - 2015-03-09   output - 2019-08-09. [ After adding 4 years]

Now, let us move to the implementation and understand the actual working of the method.

Implementation:

import java.util.Calendar;


public class DateAfterYears {


  public static void main(String[] args) {
    //creation of Calendar instance
    Calendar today = Calendar.getInstance();


System.out.println("Today's date : " + (today.get(Calendar.MONTH) + 1)
                        + "-"
                        + today.get(Calendar.DATE)
                        + "-"
                        + today.get(Calendar.YEAR));




    //adding four years to the current date using Calendar.add method
today.add(Calendar.YEAR,4);


System.out.println("The date after four years : " + (today.get(Calendar.MONTH) + 1)
                        + "-"
                        + today.get(Calendar.DATE)
                        + "-"
                        + today.get(Calendar.YEAR));






  }
}

Output:

How to Add 4 Years to the Current Date in Java

Explanation: In the above code, we have tried to add 4 years to the current date. To do so, we have taken various methods into account.

Summary:

We learned how to add four years to the current date in Java language. It was interesting to understand we can accomplish this task simply by taking certain methods such as.add(), Calender.YEAR, Calender.MONTH, Calender.DATE, and calendar.instanceinto account and providing 4 as the parameter to the .add method.

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


Related Topics

Sudoku in Java

Sudoku is a combinatorial-number-placement puzzle with a logic-based approach. The goal of a traditional Sudoku puzzle is to fill in the numbers on a 9 by 9 grid so that...

6 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Transient variable in Java

In this article, you will be acknowledged about transient variable along with its functions. We would conclude by understanding an example program about it. Transient variable By introducing the transitory keyword, we...

3 minutes read.

Creating API Document Javadoc tool

The JavaDoc utility is a document generator tool written in Java that generates standard documentation in HTML format. It parses declarations and documentation in a source file collection that describes...

3 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

Perfect Number Program in Java

Perfect Number Program in Java A perfect number is a number whose sum of all the factors, excluding the number itself, is equal to the number. For example, 28 is a...

4 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 longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Character Array in Java

A character array is an array which holds values of character data types. It is different from a string array. The character array, string, and StringBuffer classes in the Java...

4 minutes read.

String isnullorempty in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.

Java 8 Consumer Interface in Java

The Consumer Interface is used to implement the functional programming in Java. The Consumer Interface indicates a function that accepts a single input and outputs a result. These functions don’t...

2 minutes read.

Pernicious Number in Java

If the number of 1s in a number appearing in the binary representation is the prime number, such a number is known as a pernicious number. A pernicious number always corresponds to...

4 minutes read.

Java SHA256

Definition: In cryptography, SHA is a hash function that takes 20 bytes of input and produces an approximate 40-digit hexadecimal integer as the hash result. Class for Message Digest: Java's MessageDigest Class,...

2 minutes read.