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
Leave a Reply