All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Java Check If Date Is Greater Than Today

Last Updated : Mar 11, 2024

Java Check If Date Is Greater Than Today

In this article we will show you the solution of java check if date is greater than today, Java makes the determination of whether a specific date is greater than today by comparing it to the current date. There is a date representation function in Java.time.

The LocalDate class is part of the current date and time API, which was initially made available in Java 8.

This API's robust date conversion & comparison functions make working with dates easier. The java.time import is a prerequisite.

The LocalDate class makes working with dates easier than the convoluted prior date class.

The current date can then be displayed using the method, which returns the date in the system's scheduled time zone. Now move to the concept of java check if date is greater than today.

Step By Step Guide On Java Check If Date Is Greater Than Today :-

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class DateComparison {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(;
        LocalDate currentDate =;
        System.out.print("Enter a date (YYYY-MM-DD): ");
        String userInput = scanner.nextLine();
        LocalDate givenDate = parseDate(userInput);
        if (givenDate != null) {
            if (givenDate.isAfter(currentDate)) {
                System.out.println("The given date is greater than today.");
            } else if (givenDate.isEqual(currentDate)) {
                System.out.println("The given date is equal to today.");
            } else {
                System.out.println("The given date is earlier than today.");
        } else {
            System.out.println("Invalid date format. Please use the format YYYY-MM-DD.");
    private static LocalDate parseDate(String dateString) {
        LocalDate date = null;
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            date = LocalDate.parse(dateString, formatter);
        } catch (Exception e) {
        return date;
  1. Beginning with the java.time package, which enables us to work with times and dates, we import the essential classes.
  2. To read user input, we also import the java.util.Scanner class.
  3. We create a DateComparison public class, which will be our program's entry point.
  4. To read user input, we construct a Scanner object called a scanner inside the main method.
  5. Utilizing, we obtain the current date and save it in the currentDate variable.
  6. We ask the user to enter a date in the format "YYYY-MM-DD" and then read the data using scanner.nextLine().
  7. The userInput variable holds the user input.
  8. Then we use the helper method parseDate() which transform the user-provided string into a LocalDate object.
  9. After that this function creates an attempt to parse the text with the help of the given date format ("yyyy-MM-dd") and, if successful, returns a LocalDate object; otherwise, it returns null.
  10. We now determine whether the givenDate is not null.
  11. If it is null, it indicates that the input format contained an error, and we then show an error notice. If not, we compare the dates after that.
  12. We utilize the isAfter() method to see whether givenDate follows currentDate inside the if block.
  13. In that case, we print a message indicating that the specified date is older than today.
  14. We use the isEqual() method to check for equality and output the appropriate message if the dates are equal.
  15. Both conditions must be satisfied in order for the given date to be earlier than today.
  16. To free up system resources, we shut down the scanner.
  17. Then the parseDate() function tries to convert a string into a LocalDate object with the help of the provided date format ("yyyy-MM-dd"), is the last one we define.
  18. In the end the event that a parsing an exception which occurs, then we catch it & turns it into empty to denote a failure.

Conclusion :-

As a result, we have successfully acquired the Java check if date is larger than today concept.

Additionally, we discovered the java.time.Java's LocalDate class offers a simple and effective method for handling dates.

We can quickly discover whether a date is greater than, equal to, or earlier than today by comparing the given date with the current date.

I hope this article on java check if date is greater than today helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪