Check a Given Date is Past, Today or Future’s Date in Java 8

In this example I am going to show you how to check if an input date is past date or today’s date or future date. I am using Java version 12 and for this example you should use at least Java version 8. Java 8 or higher version provides thread safe version of date time API that can be used safely in multi-threaded environment.

If you want to calculate the future or past date by adding or removing days to the input or given date then you can read the example on Future or Past date in Java or How to add or subtract days, weeks, months, years in Java 8 Date. In this example I will check only whether the data is future, past or today’s date in Java.

Prerequisites

Java at least 8

Check Past, Future or Today

Java 8 onward date API provides isBefore(), isEqual(), isAfter() and these methods take an input date value which is then compared to another date value to check whether the input date is before, equal or after another date value.

package com.roytuts.java.check.date.future.or.past;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class DateFutureOrPastApp {

	public static void main(String[] args) {

		final String pastMsg = "The input date is a past date";
		final String todayMsg = "The input date is a today's date";
		final String futureMsg = "The input date is a future date";

		boolean isDatePast = isDatePast("2020-12-23", "yyyy-MM-dd");
		boolean isDateToday = isDateToday("2020-12-23", "yyyy-MM-dd");
		boolean isDateFuture = isDateFuture("2020-12-23", "yyyy-MM-dd");

		if (isDatePast) {
			System.out.println(pastMsg);
		}

		if (isDateToday) {
			System.out.println(todayMsg);
		}

		if (isDateFuture) {
			System.out.println(futureMsg);
		}

		isDatePast = isDatePast("2020-12-29", "yyyy-MM-dd");
		isDateToday = isDateToday("2020-12-29", "yyyy-MM-dd");
		isDateFuture = isDateFuture("2020-12-29", "yyyy-MM-dd");

		if (isDatePast) {
			System.out.println(pastMsg);
		}

		if (isDateToday) {
			System.out.println(todayMsg);
		}

		if (isDateFuture) {
			System.out.println(futureMsg);
		}

		isDatePast = isDatePast("2020-12-31", "yyyy-MM-dd");
		isDateToday = isDateToday("2020-12-31", "yyyy-MM-dd");
		isDateFuture = isDateFuture("2020-12-31", "yyyy-MM-dd");

		if (isDatePast) {
			System.out.println(pastMsg);
		}

		if (isDateToday) {
			System.out.println(todayMsg);
		}

		if (isDateFuture) {
			System.out.println(futureMsg);
		}

	}

	public static boolean isDatePast(final String date, final String dateFormat) {
		LocalDate localDate = LocalDate.now(ZoneId.systemDefault());

		DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat);
		LocalDate inputDate = LocalDate.parse(date, dtf);

		return inputDate.isBefore(localDate);
	}

	public static boolean isDateToday(final String date, final String dateFormat) {
		LocalDate localDate = LocalDate.now(ZoneId.systemDefault());

		DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat);
		LocalDate inputDate = LocalDate.parse(date, dtf);

		return inputDate.isEqual(localDate);
	}

	public static boolean isDateFuture(final String date, final String dateFormat) {
		LocalDate localDate = LocalDate.now(ZoneId.systemDefault());

		DateTimeFormatter dtf = DateTimeFormatter.ofPattern(dateFormat);
		LocalDate inputDate = LocalDate.parse(date, dtf);

		return inputDate.isAfter(localDate);
	}
}

Running the above code will give you the following output:

The input date is a past date
The input date is a today's date
The input date is a future date

That’s all about checking the given date is past, future or today’s date.

Source Code

Download

Leave a Comment