Q:

Java program to add years to the current date

belongs to collection: Java Date and Time Programs

0

Java program to add years to the current date

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to add years to the current date is given below. The given program is compiled and executed successfully.

// Java program to add years to the 
// current date

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();

    cal.add(Calendar.YEAR, 5);
    System.out.println("Date after 5 years: " + cal.getTime());
  }
}

Output:

Date after 5 years: Fri Apr 02 05:59:18 GMT 2027

Explanation:

In the above program, we created a class Main that contains a static method main(). The main() method is the entry point for the program, here we created the object of the Calendar class and added the 5 years to the current date using add() method, and printed the result.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Date and Time Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to compare time using compareTo() met... >>
<< Java program to add months to the current date...