import java.time.LocalDate;
public class ExDate {
public static void main(String[] args) {
// create objects of the different dates.
LocalDate date = LocalDate.now();
LocalDate yesterday = date.minusDays(1);
LocalDate tomorrow = yesterday.plusDays(2);
// print today,yesterday and tomorrow date.
System.out.println("Today date is : " + date);
System.out.println("Yesterday date is : " + yesterday);
System.out.println("Tommorow date is : " + tomorrow);
}
}
Output
Today date is : 2017-11-30
Yesterday date is : 2017-11-29
Tommorow date is : 2017-12-01
In this example, we are going to print the dates of different days. For print dates, we are creating an object of LocalDate class this class represents date in default format 'yyyy-mm-dd'. This is given in java package that LocalDate date = LocalDate.now() will be used for present date (today), LocalDate yesterday =date.minusDays(1) will return a copy of LocalDate with specified number of days subtracted.
And LocalDate tomorrow = yesterday.plusDays(2) will return a copy of LocalDate with specified number of days added.
After that print these days System.out.println("Today date is : "+date) for present date , System.out.println("Yesterday date is : "+yesterday) for yesterday System.out.println("Tomorrow date is : "+tomorrow) for tomorrow .
Program
Output
In this example, we are going to print the dates of different days. For print dates, we are creating an object of LocalDate class this class represents date in default format 'yyyy-mm-dd'. This is given in java package that LocalDate date = LocalDate.now() will be used for present date (today), LocalDate yesterday =date.minusDays(1) will return a copy of LocalDate with specified number of days subtracted.
And LocalDate tomorrow = yesterday.plusDays(2) will return a copy of LocalDate with specified number of days added.
After that print these days System.out.println("Today date is : "+date) for present date , System.out.println("Yesterday date is : "+yesterday) for yesterday System.out.println("Tomorrow date is : "+tomorrow) for tomorrow .