The Java LocalDate class can represent dates, with methods to get today’s date, add/subtract days/weeks/years to calculate future or past dates, and print the results. Manipulating dates is straightforward with LocalDate’s intuitive API.
Here is a 4 line summary of how to print future and past dates using LocalDate in Java:
- Import Java time classes:
import java.time.LocalDate;
- Create LocalDate instance:
LocalDate date = LocalDate.now();
- Print future/past dates:
date = date.plusDays(1);
ordate = date.minusWeeks(2);
- Print result:
System.out.println(date);
Java Program
package TestAutomationCentral;
import java.time.LocalDate;
public class LocalDateDemo {
public static void main(String[] args) {
//Visit - TestAutomationCentral.com
// How to print future and past date using LocalDate
LocalDate today = LocalDate.now();
System.out.println(today);
System.out.println(today.plusDays(1));
System.out.println(today.plusDays(-1));
}
}