- Instant help with your Java coding problems

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 .

Share "How to get yesterday's date in Java?"