All TalkersCode Topics

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

Compare Date With Current Date In Java

Last Updated : Mar 11, 2024

Compare Date With Current Date In Java

In this article we will show you the solution of compare date with current date in java, Programmers frequently work with dates, and we frequently need to compare past dates to the present day to establish whether an event has already happened or is still to come.

We will look at how to compare dates with the current date in Java in this lesson.

To make sure that everyone understands the procedure, we will give detailed explanations of the code.

We may use a simple method in Java to compare a date to the current date.

The first step is to use the java.util.Date class to get the current date. Then, we can use a variety of comparison techniques offered by these classes to compare the target date with the present date.

Step By Step Guide On Compare Date With Current Date In Java :-

package task;
import java.util.Date;
public class DateCompare {
    public static void main(String[] args) {
        Date CD = new Date();
        Date TD = new Date(123,6,14); //year 1900+123=2023
        int CR = TD.compareTo(CD);
        if (CR < 0) {
            System.out.println("Target date is before the current date.");
        } else if (CR > 0) {
            System.out.println("Target date is after the current date.");
        } else {
            System.out.println("Target date is equal to the current date.");
  1. The "task" Java package is declared.
  2. It includes the java.util.Date import working with dates in a date class.
  3. The program's entry point, the main method, can be found in the DateCompare class.
  4. Two Date objects are created in the main method: CD (current date) and TD (target date).
  5. The TD object is initialised with the specified year, month, and day values using the Date constructor. The goal date in this instance is set at June 14, 2023.
  6. The TD object and CD object are then compared using the compareTo function of the Date type. The CR variable holds the comparison's outcome.
  7. Based on the outcome of the comparison, the if-else phrases are utilised to print a relevant message. The target date is earlier than the current date if CR is less than 0. If the CR value is more than 0, the target date is one that is later than the present. If CR is equal to 0, the goal date and the present date are same.
  8. Finally, based on the comparison result, the relevant message is printed.

Conclusion: :-

This tutorial taught us how to compare dates in Java with the current date. We looked into methods that used the java.util.Date class for earlier Java releases.

We may quickly detect whether a date is before, after, or equal to the current date by using comparison techniques offered by these classes.

This information is useful for a number of purposes, including work scheduling and event date verification.

Use the correct class according to your Java version and benefit from Java's strong date and time APIs by doing so.

I hope this article on compare date with current date in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪