Javaexercise.com

How to increment a date by one day in Java?

In Java, if you want to increment a date by one day, you don't need to worry because we have several solutions for you. In this article, we will use the latest Java 8 date time API, util date, and Calender class to increment a date by one day. Let's get started.

Latest Java 8+ Solution:

Increment a date by one day by using plusDays() Method in Java

In this code, we used LocalDate class and now() method to get the current date. The plusDays() method is used to increment one day in the current date. We can add any number of days to the date , but for now, we add 1. 

import java.time.LocalDate;
/* 
 *  Code example to increment a day to date in Java
 */
public class JExercise {
	public static void main(String[] args) {
		// Current Date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current Date: "+localDate);
		// Add Weeks
		LocalDate newLocalDate = localDate.plusDays(1);
		System.out.println("Date After incrementing a Day: "+newLocalDate);
	}
}

Output:

Current Date: 2021-06-12
Date After incrementing a Day: 2021-06-13
 

Increment a date by one day by using Calendar Class in Java

If you are working with the Calender class and want to add a day to the date, simply use add() method of it that takes two arguments, the first is a Date constant, and the second is the number of days you want to increment. See the code example below.

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
/* 
 *  Code example to increment a day to date in Java
 */
public class JExercise {
	public static void main(String[] args) throws ParseException {
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		System.out.println("Date: "+date);
        cal.setTime(date);
        cal.add(Calendar.DATE, 1);
        Date newDate = cal.getTime();
		System.out.println("Date After incrementing a Day: "+newDate);
	}
}

Output:

Date: Sat Jun 12 22:49:33 IST 2021
Date After incrementing a Day: Sun Jun 13 22:49:33 IST 2021
 

Format the date result

Since the above date result is a little lengthy, you can use SimpleDateFormat class to format the date as per your required format. 

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = format.format(newDate);
System.out.println(formattedDate);

 

Increment a date by one day by using plusSeconds() Method in Java

This is another solution where you can increment a date by adding the seconds. The plusSeconds() method can be used if you have time in seconds. We used the Instant class of Java 8 date time API to get the current date in Java. See the code below.

import java.text.ParseException;
import java.time.Instant;
import java.util.Date;
/* 
 *  Code example to increment a day to a date in Java
 */
public class JExercise {
	public static void main(String[] args) throws ParseException {
		System.out.println("Current Date :"+Instant.now());
		Date date  = Date.from(Instant.now().plusSeconds(86400));
		System.out.println("Date After incrementing a Day: "+date);
	}
}

Output:

Current Date :2021-06-12T17:33:03.222598Z
Date After incrementing a Day: Sun Jun 13 23:03:03 IST 2021
 

Increment a date by one day by using Apache Commons Library in Java

If you are working with Apache library and want to increment a date by one day, then use the addDays() method of DateUtils class. This method takes two arguments, the first is the date, and the second is the number of days you want to increment. It returns a date object after adding the day. See the code below.

import java.text.ParseException;
import java.util.Date;
import org.apache.commons.lang3.time.DateUtils;
/* 
 *  Code example to increment a day to a date in Java
 */
public class JExercise {
	public static void main(String[] args) throws ParseException {
		Date date = new Date();
		System.out.println("Date :"+date);
		Date nextDate = DateUtils.addDays(date, 1);
		System.out.println(nextDate);
	}
}

Output:

Date :Sat Jun 12 23:12:47 IST 2021
Sun Jun 13 23:12:47 IST 2021