Java 8 – How to get next and previous date ?

In this article, we will learn how to get next and previous date for a given Date

Get Next and Previous Date :

  • Using LocalDate
  • Using LocalDateTime
  • Using Date/Calendar

1. LocalDate – get Next and Previous Date :

  • First, get current LocalDate using LocalDate.now() method
    1. For getting next LocalDate add 1 Day to current date using plusDays(1) method of LocalDate
    2. For getting previous LocalDate subtract 1 Day from current date using minusDays(1) method of LocalDate
  • Finally, print current, next and previous LocalDate to the console

GetNextAndPreviousLocalDate.java

package in.bench.resources.java8.current.date.time;

import java.time.LocalDate;

public class GetNextAndPreviousLocalDate {

	public static void main(String[] args) {

		// 1. get Current LocalDate
		LocalDate localDate = LocalDate.now();
		System.out.println("Current Day is :- \n" 
				+ localDate);


		// 2. get Next LocalDate
		LocalDate localDateNext = localDate.plusDays(1);
		System.out.println("\nNext Day is :- \n" 
				+ localDateNext);


		// 3. get Previous LocalDate
		LocalDate localDatePrevious = localDate.minusDays(1);
		System.out.print("\nPrevious Day is :- \n" 
				+ localDatePrevious);
	}
}

Output:

Current Day is :- 
2022-09-13

Next Day is :- 
2022-09-14

Previous Day is :- 
2022-09-12

2. LocalDateTime – get Next and Previous Date :

  • First, get current LocalDateTime using LocalDateTime.now() method
    1. For getting next LocalDateTimeadd 1 Day to current date using plusDays(1) method of LocalDateTime
    2. For getting previous LocalDateTimesubtract 1 Day from current date using minusDays(1) method of LocalDateTime
  • Finally, print current, next and previous LocalDateTime to the console

GetNextAndPreviousLocalDateTime.java

package in.bench.resources.java8.current.date.time;

import java.time.LocalDateTime;

public class GetNextAndPreviousLocalDateTime {

	public static void main(String[] args) {

		// 1. get Current LocalDateTime
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Current Day date/time is :- \n" 
				+ localDateTime);


		// 2. get Next LocalDate
		LocalDateTime localDateNext = localDateTime.plusDays(1);
		System.out.println("\nNext Day date/time is :- \n" 
				+ localDateNext);


		// 3. get Previous LocalDate
		LocalDateTime localDatePrevious = localDateTime.minusDays(1);
		System.out.print("\nPrevious Day date/time is :- \n" 
				+ localDatePrevious);
	}
}

Output:

Current Day date/time is :- 
2022-09-13T20:00:30.395656800

Next Day date/time is :- 
2022-09-14T20:00:30.395656800

Previous Day date/time is :- 
2022-09-12T20:00:30.395656800

3. Date/Calendar – get Next and Previous Date :

  • First, get current Date by instantiating Date object
    1. For getting next Day – extract current date/time in milliseconds using getTime() method of Date and add 1 day i.e.; (24 * 60 * 60 * 1000 = 86400000 milliseconds)
    2. For getting previous Day – extract current date/time in milliseconds using getTime() method of Date and subtract 1 day i.e.; (24 * 60 * 60 * 1000 = 86400000 milliseconds)
  • Finally, print current, next and previous Date to the console

GetNextAndPreviousDate.java

package in.bench.resources.java8.current.date.time;

import java.util.Date;

public class GetNextAndPreviousDate {

	public static void main(String[] args) {

		// Milliseconds in a Day
		long millisInADay = 24 * 60 * 60 * 1000;


		// 1. get Current Date/time
		Date current = new Date();
		System.out.println("Current Day date/time is :- \n" + current);


		// 2. get Next Date/time
		Date nextDate = new Date(current.getTime() + millisInADay);
		System.out.println("\nNext Day date/time is :- \n" + nextDate);


		// 3. get Previous Date/time
		Date previousDate = new Date(current.getTime() - millisInADay);
		System.out.print("\nPrevious Day date/time is :- \n" + previousDate);
	}
}

Output:

Current Day date/time is :- 
Tue Sep 13 20:00:43 IST 2022

Next Day date/time is :- 
Wed Sep 14 20:00:43 IST 2022

Previous Day date/time is :- 
Mon Sep 12 20:00:43 IST 2022

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java 8 – How to check whether a given Date is weekend ?
Java 8 – How to check whether a given Date/Year is leap year ?