BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java – Add days to Date

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial we will see how to add Days to the date in Java.

Table of contents

1. Adding Days to the given Date using Calendar class
2. Adding Days to the current date using Calendar class
3. Add One day to a Date in Java
4. Add One day to the current date in Java
5. Java 8 – Adding 1 day or number of days to the current or given date

1. Adding Days to the given Date using Calendar class

In this example we have given a date “2017-01-29” 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 = "2017-01-29";  
	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: 2017-01-29
Date after Addition: 2017-02-05

2. Adding Days to the current Date using Calendar class

This is similar to the above example except that here we are adding the 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: 2017/10/18
Date after Addition: 2017/10/25

3. Java – Increment a Date by 1 Day

The reason I have mentioned this in a separate heading is because people ask this a lot. We have already seen how to add days to a date in Java in the above programs. To increment the date or current date by one, you just need to add 1 day to the date.

3.1 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

3.2 Add One Day to the current date

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);
   }
}

4. Java 8 – Adding Days to Date

In java 8, it is so easy to add days to a date without Calendar class. Here we use the LocalDate class provided in the new package java.time which is introduced 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

Related Articles:

1. Java – How to get current Date and Time
2. Getting current date and time in Java 8
3. Java Date format
4. Java Date Parsing
5. Java 8 – Calculate Days between two given dates

Top Related Articles:

  1. Date Formatting In Java With Time Zone
  2. Java – Date Format to display the Day of the week
  3. Date validation in java
  4. How to Call a Method in Java
  5. Java Date and Time

Tags: Java-Date

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap