Java Check If Date Is Greater Than Today
Last Updated : Mar 11, 2024
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 LocalDate.now() 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(System.in); LocalDate currentDate = LocalDate.now(); 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."); } scanner.close(); } 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; } }
- Beginning with the java.time package, which enables us to work with times and dates, we import the essential classes.
- To read user input, we also import the java.util.Scanner class.
- We create a DateComparison public class, which will be our program's entry point.
- To read user input, we construct a Scanner object called a scanner inside the main method.
- Utilizing LocalDate.now(), we obtain the current date and save it in the currentDate variable.
- We ask the user to enter a date in the format "YYYY-MM-DD" and then read the data using scanner.nextLine().
- The userInput variable holds the user input.
- Then we use the helper method parseDate() which transform the user-provided string into a LocalDate object.
- 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.
- We now determine whether the givenDate is not null.
- 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.
- We utilize the isAfter() method to see whether givenDate follows currentDate inside the if block.
- In that case, we print a message indicating that the specified date is older than today.
- We use the isEqual() method to check for equality and output the appropriate message if the dates are equal.
- Both conditions must be satisfied in order for the given date to be earlier than today.
- To free up system resources, we shut down the scanner.
- 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.
- 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.