Java Program to add days to current date using Java Calendar

Java 8Object Oriented ProgrammingProgramming

Import the following package for Calendar class in Java

import java.util.Calendar;

Firstly, create a Calendar object and display the current date

Calendar calendar = Calendar.getInstance();
System.out.println("Current Date = " + calendar.getTime());

Now, let us add days using the add() method and Calendar.DATE constant

calendar.add(Calendar.DATE, 2);

The following is an example

Example

 Live Demo

import java.util.Calendar;
public class Demo {
   public static void main(String[] args) {
      Calendar calendar = Calendar.getInstance();
      System.out.println("Current Date = " + calendar.getTime());
      // Incrementing Days by 2
      calendar.add(Calendar.DATE, 2);
      System.out.println("Updated Date = " + calendar.getTime());
  }
}

Output

Current Date = Thu Nov 22 16:04:55 UTC 2018
Updated Date = Sat Nov 24 16:04:55 UTC 2018
raja
Published on 19-Dec-2018 15:10:52
Advertisements