Add days to date in Java

In this tutorial we are going to learn about add days to date in Java.

Introduction

In java application adding days to date is a task which we use in scheduling systems, calendar applications, or any other type of software that handles dates and times. Java provides several ways to manipulate dates and times.

Adding Days to the given Date using Calendar class

We are going to take date 20-11-15 as an example and we are adding the days to it using Calendar class.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;
class Example{
   public static void main(String args[]){
    //Given Date in String format
    String oldDate = "2020-11-15";  
    System.out.println("Date before Addition: "+oldDate);
    //Specifying date format that matches the given date
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    try{
       //Setting the date to the given date
       c.setTime(sdf.parse(oldDate));
    }catch(ParseException e){
        e.printStackTrace();
     }
       
    //Number of Days to add
    c.add(Calendar.DAY_OF_MONTH, 7);  
    //Date after adding the days to the given date
    String newDate = sdf.format(c.getTime());  
    //Displaying the new Date after addition of Days
    System.out.println("Date after Addition: "+newDate);
   }
}

Output:

Date before Addition: 2020-11-15
Date after Addition: 2020-11-22

=== Code Execution Successful ===

 Adding Days to the current Date using Calendar class

This concept is slightly similar to the above example is that we are addingthe days to the current date instead of given date.

import java.text.SimpleDateFormat;
import java.util.Calendar;
class Example{
   public static void main(String args[]){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    //Getting current date
    Calendar cal = Calendar.getInstance();
    //Displaying current date in the desired format
    System.out.println("Current Date: "+sdf.format(cal.getTime()));
       
    //Number of Days to add
        cal.add(Calendar.DAY_OF_MONTH, 7);  
    //Date after adding the days to the current date
    String newDate = sdf.format(cal.getTime());  
    //Displaying the new Date after addition of Days to current date
    System.out.println("Date after Addition: "+newDate);
   }
}

 

Output:

Current Date: 2024/05/21
Date after Addition: 2024/05/28

=== Code Execution Successful ===

 Increment a Date by 1 Day

From the above examples we have added days to a date in java program. Now we are going to increment the date or current date by one.

Add One Day to a given date

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;
class Example{
   public static void main(String args[]){
        String oldDate = "2017-01-29";  
    System.out.println("Date before Addition: "+oldDate);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    try{
       c.setTime(sdf.parse(oldDate));
    }catch(ParseException e){
       e.printStackTrace();
     }
    //Incrementing the date by 1 day
    c.add(Calendar.DAY_OF_MONTH, 1);  
    String newDate = sdf.format(c.getTime());  
    System.out.println("Date Incremented by One: "+newDate);
   }
}

Output:

Date before Addition: 2017-01-29
Date Incremented by One: 2017-01-30


Add One Day to the current date

Now we are going to add one day to the current date in java program.

import java.text.SimpleDateFormat;
import java.util.Calendar;
class Example{
   public static void main(String args[]){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    Calendar cal = Calendar.getInstance();
    System.out.println("Current Date: "+sdf.format(cal.getTime()));
    //Adding 1 Day to the current date
    cal.add(Calendar.DAY_OF_MONTH, 1);  
    //Date after adding one day to the current date
    String newDate = sdf.format(cal.getTime());  
    //Displaying the new Date after addition of 1 Day
    System.out.println("Incremnted current date by one: "+newDate);
   }
}

Output:

Current Date: 2024/05/21
Incremnted current date by one: 2024/05/22

=== Code Execution Successful ===

Adding Days to Date

Now we are going to add days to the date without class calendar. But we’ll use LocalDate class provided in the new package java.time in java 8.

import java.time.LocalDate;
class Example{
   public static void main(String args[]){
       //Adding one Day to the current date
       LocalDate date =  LocalDate.now().plusDays(1);
       System.out.println("Adding one day to current date: "+date);
      
    //Adding number of Days to the current date
    LocalDate date2 =  LocalDate.now().plusDays(7);
    System.out.println("Adding days to the current date: "+date2);
      
    //Adding one Day to the given date
    LocalDate date3 = LocalDate.of(2016, 10, 14).plusDays(1);
    System.out.println("Adding one day to the given date: "+date3);
      
    //Adding number of Days to the given date
    LocalDate date4 = LocalDate.of(2016, 10, 14).plusDays(9);
    System.out.println("Adding days to the given date: "+date4);
   }
}

Output:
Adding one day to current date: 2017-10-19
Adding days to the current date: 2017-10-25
Adding one day to the given date: 2016-10-15
Adding days to the given date: 2016-10-23

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top