Check two dates are equal in Java

In this source code example, we show you how to check two dates are equal in Java.

Check two dates are equal in Java

package com.ramesh.java8.datetime;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

/**
 * Useful Java8DateUtiliy Methods
 * @author javaguides.net
 *
 */

public final class Java8DateUtility {
    /**
     * Check two dates are equals.
     * @param date
     * @param today
     * @return
    */
    public static boolean checkDateEquals(LocalDate date, LocalDate today) {
        if (date.equals(today)) {
           System.out.printf("Today %s and date1 %s are same date %n", today, date);
           return true;
        }
        return false;
    }
}

JUnit test case

package com.ramesh.java8.datetime;

import static org.junit.Assert.assertTrue;

import java.time.LocalDate;

import org.junit.Test;

/**
 * JUnit test cases for Java8DateUtiliy Methods
 * @author javaguides.net
 *
 */
public class Java8DateUtilityTest {

    @Test
    public void checkDateEqualsTest() {
        assertTrue(Java8DateUtility.checkDateEquals(LocalDate.now(), LocalDate.now()));
    }
}
Run the JUnit test cases will print the output:
Today 2018-07-21 and date1 2018-07-21 are same date 

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course