×

Java Programs

Java Practice

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

In this java program, we have to print the different dates of days. Here we are using package java.time.Localdate for using the method and class of this package.
Submitted by IncludeHelp, on December 04, 2017

We are here taking three days like date, yesterday and tomorrow.

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 .

Java Date and Time Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.