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
- For getting next LocalDate – add 1 Day to current date using plusDays(1) method of LocalDate
- 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
- For getting next LocalDateTime – add 1 Day to current date using plusDays(1) method of LocalDateTime
- For getting previous LocalDateTime – subtract 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
- 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)
- 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 :
- Java 8 – How to get current Date in different ways ?
- Java 8 – How to convert java.util.Date to LocalDate and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to OffsetDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to Instant and vice-versa ?
- Java 8 – How to convert java.util.Date to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert java.util.Date to Calendar and vice-versa ?
- Java 8 – How to convert java.util.Date to GregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to XMLGregorianCalendar and vice-versa ?
- Java 9 – How to convert java.util.Date to LocalDate using ofInstant() method ?
- Java 9 – How to convert java.util.Date to LocalTime using ofInstant() method ?
- Java – How to convert java.util.Date to String in different formats ?
- Java – How to convert String to java.util.Date in different formats ?
- Java – How to get Date/time with AM/PM marker and Zone ?
- Java 8 – How to get all dates between two Dates ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!