Get yesterday's date in Java
Question:
How to get yesterday's date in Java? Answer:
LocalDate today = LocalDate.now();
LocalDate yesterday = today.minusDays(1);
// In string format
LocalDate today = LocalDate.now();
String yesterday = (today.minusDays(1)).format(DateTimeFormatter.ISO_DATE);
Description:
Using the LocalDate
class - introduced in Java 8 - it is quite simple to get yesterday's date. It's almost the same as getting tomorrow's date.
First, create today's date by creating a new LocalDate
using the now()
static method. Then simply extract one day to the actual date using the minusDays()
method and that's it.
You can format the result as you want using the format()
method and the appropriate DateTimeFormatter
.
Reference:
LocalDate minusDays reference
Share "How to get yesterday's date in Java?"
Related snippets:
- Get the last Friday of a month in Java
- Get the first Monday of a month in Java
- Get the first day of the week in Java
- Get the first day of the current month in Java
- Get the last day of a month in Java
- Get the number of days between two dates in Java
- Get yesterday's date in Java
- Get tomorrow's date in Java
- Get actual date in Java
Tags:
yesterday's date, yesterdays date, get day before today Technical term:
Get yesterday's date in Java