How to get the Yesterday and Tomorrow Date in Java

Get the yesterday and tomorrow date in Java

In this example program, We will show you about, How to get the yesterday and tomorrow Date in Java. The program has been tested and output is shared in the same post.

#1 Getting the Yesterday Date

public static Date getYesterdayDate(Date date) {
Date yesterdayDate = null;
if (date != null) {
Calendar c = Calendar.getInstance();
c.setTime(date); // Setting the today date
c.add(Calendar.DATE, -1); // Decreasing 1 day
yesterdayDate = c.getTime();
}
return yesterdayDate;
}

#2 Getting the Tomorrow Date

// Method to get tomorrow Date
public static Date getTomorrowDate(Date date) {
Date tomorrowDate = null;
if (date != null) {
Calendar c = Calendar.getInstance();
c.setTime(date); // Setting the today date
c.add(Calendar.DATE, 1); // Increasing 1 day
tomorrowDate = c.getTime();
}
return tomorrowDate;
}

#3 Formatting the Results

private static String dateFormat = "dd/MM/yyyy";
public static String getFormattedDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String formatedDate = null;
if (date != null) {
formatedDate = sdf.format(date);
}
return formatedDate;
}

#4 Final Example (It`s all over)

package com.dineshkrish;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 
* @author Dinesh Krishnan
*
*/
public class TomorrowDate {
private static String dateFormat = "dd/MM/yyyy";
// Method to get tomorrow Date
public static Date getTomorrowDate(Date date) {
Date tomorrowDate = null;
if (date != null) {
Calendar c = Calendar.getInstance();
c.setTime(date); // Setting the today date
c.add(Calendar.DATE, 1); // Increasing 1 day
tomorrowDate = c.getTime();
}
return tomorrowDate;
}
// Method to get yesterday date
public static Date getYesterdayDate(Date date) {
Date yesterdayDate = null;
if (date != null) {
Calendar c = Calendar.getInstance();
c.setTime(date); // Setting the today date
c.add(Calendar.DATE, -1); // Decreasing 1 day
yesterdayDate = c.getTime();
}
return yesterdayDate;
}
// Method to format the give date
public static String getFormattedDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
String formatedDate = null;
if (date != null) {
formatedDate = sdf.format(date);
}
return formatedDate;
}
public static void main(String[] args) {
// Current Date
Date todayDate = new Date();
// Getting the tomorrow date
Date tomorrowDate = getTomorrowDate(todayDate);
// Getting the yesterday date
Date yesterdayDate = getYesterdayDate(todayDate);
// Printing all the results
System.out.println("Yesterday Date is : "+getFormattedDate(yesterdayDate));
System.out.println("Today Date is : "+getFormattedDate(todayDate));
System.out.println("Tomorrow Date is : "+getFormattedDate(tomorrowDate));
}
}

Output

The following output will give you idea about, How to get the yesterday and tomorrow date in java programming.

————-
Yesterday Date is : 27/12/2016
Today Date is : 28/12/2016
Tomorrow Date is : 29/12/2016

References

1. Java Date API
2. Java Calendar API
3. Java SimpleDateFormat API

No Responses

Leave a Reply

Your email address will not be published.