- Instant help with your Java coding problems

Get yesterday's date in Java

How to get yesterday's date in Java?
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);

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?"