Q:

Java program to print different dates of days like today, yesterday and tomorrow

belongs to collection: Java Date and Time Programs

0

Java program to print different dates of days like today, yesterday and tomorrow

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program

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 .

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Date and Time Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to print the months in different form... >>
<< How to get current system date and time in Java?...