×

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

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

8 minutes read.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

The Diamond Operator in Java 7

The Diamond operator is a comparatively recent Java operator originally developed in JDK 7 to enhance type identification and eliminate boilerplate Java code. The Diamond operator is characterized by a...

2 minutes read.

Java List Node

In Java, List Node is the same as the single linked list, which is the collection of nodes. So, we can say, the list nodes are grouped together to get...

8 minutes read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Java calculate age

In this section, we will create a Java program that calculates age from the given date of birth or current date. In order to get the date of birth from the current...

6 minutes read.

Can we override Private Method in Java

In this article we will learn the concept of override, private method in java and we will now whether we can override private method in java or not. Override in Java Any...

3 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

Java Math IEEEremainder() Method

The IEEEremainder() method of Math class calculates the remainder as prescribed by the IEEE754 standard. This method simply returns the remainder when f1 (dividend) is divided by f2 (divisor). Syntax: public static...

2 minutes read.

Java Map Generic

Java arrays maintain an ordered collection of things, and the index can be used to access the data (an integer). Unlike HashMap, which stores data as a Key/Value pair. We...

3 minutes read.

Java Math atan() Method

The atan() method of Math class computes the trigonometric Arc Tangent (inverse of tangent ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double atan(double a) Parameters: The...

2 minutes read.