Close

AssertJ - Date and Time Assertions

[Last Updated: Mar 3, 2026]

AssertJ provides assertion methods for all major Java 8 date-time types from the java.time package, including LocalDate, LocalDateTime, ZonedDateTime, and Instant. These assertions go beyond equality, supporting before/after comparisons, period checks, and truncated comparisons.

Why Not Just Use isEqualTo?

Date-time values are often generated dynamically (e.g., timestamps set to "now"). Exact equality comparisons are brittle because the millisecond value may differ between test setup and execution. Methods like isBefore, isAfter, isCloseTo, and isBeforeOrEqualTo let you express approximate or relative constraints robustly.

Key Methods

isBefore, isAfter, isBeforeOrEqualTo, isAfterOrEqualTo, isEqualTo, isIn, hasDayOfMonth, hasMonth, hasYear.

Example

package com.logicbig.example;

import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import static org.assertj.core.api.Assertions.*;

public class DateTimeAssertionsExample {

    @Test
    void myTest() {
        LocalDate date = LocalDate.of(2024, Month.JUNE, 15);

        // before / after
        assertThat(date).isBefore(LocalDate.of(2025, 1, 1))
                        .isAfter(LocalDate.of(2023, 1, 1));
        System.out.println("Before/after passed");

        // field checks
        assertThat(date).hasYear(2024)
                        .hasMonth(Month.JUNE)
                        .hasDayOfMonth(15);
        System.out.println("Field checks passed");

        LocalDateTime dt = LocalDateTime.of(2024, 6, 15, 10, 30);
        assertThat(dt).isAfter(LocalDateTime.of(2024, 1, 1, 0, 0))
                      .hasHour(10)
                      .hasMinute(30);
        System.out.println("LocalDateTime checks passed");
    }
}

Output

$ mvn clean test -Dtest=*
[INFO] Scanning for projects...
[INFO]
[INFO] ---------< com.logicbig.example:assertj-date-time-assertions >----------
[INFO] Building assertj-date-time-assertions 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ assertj-date-time-assertions ---
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ assertj-date-time-assertions ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-date-time-assertions\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ assertj-date-time-assertions ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ assertj-date-time-assertions ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-date-time-assertions\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ assertj-date-time-assertions ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 1 source file with javac [debug target 17] to target\test-classes
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ assertj-date-time-assertions ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.logicbig.example.DateTimeAssertionsExample
Before/after passed
Field checks passed
LocalDateTime checks passed
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.083 s -- in com.logicbig.example.DateTimeAssertionsExample
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.281 s
[INFO] Finished at: 2026-03-03T14:23:34+08:00
[INFO] ------------------------------------------------------------------------

Conclusion

The output confirms before/after and field-level date assertions all pass. The example demonstrates how AssertJ handles temporal types natively, making it easy to express "this event should have happened in 2024" or "this date is before the deadline" without manual comparison logic.

Example Project

Dependencies and Technologies Used:

  • assertj-core 3.27.7 (Rich and fluent assertions for testing in Java)
  • junit-jupiter-engine 6.0.2 (Module "junit-jupiter-engine" of JUnit)
  • JDK 17
  • Maven 3.9.11

AssertJ - Date and Time Assertions Select All Download
  • assertj-date-time-assertions
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • DateTimeAssertionsExample.java

    See Also

    Join