Java Instant.isBefore() Method with Examples

Tags: Java Instant Java 8

In this Java core tutorial we learn how to use the java.time.Instant.isBefore() method to check if an Instant object is before another Instant object or not in Java programming language.

How to use Instant.isBefore() method

In Java, to check if an Instant object is after another Instant object or not we can use the Instant.isBefore(Instant otherInstant) method as Java code below.

InstantIsBeforeExample1.java

import java.time.Instant;

public class InstantIsBeforeExample1 {
    public static void main(String... args) {
        Instant instant1 = Instant.parse("2022-07-12T08:00:30Z");
        Instant instant2 = Instant.parse("2022-07-11T08:00:30Z");
        Instant instant3 = Instant.parse("2022-07-15T08:00:30Z");

        boolean result1 = instant1.isBefore(instant2);
        boolean result2 = instant1.isBefore(instant3);

        System.out.println("instant1: " + instant1);
        System.out.println("instant2: " + instant2);
        System.out.println("instant3: " + instant3);
        System.out.println("instant1 is before instant2: " + result1);
        System.out.println("instant1 is before instant3: " + result2);
    }
}
The output as below.
instant1: 2022-07-12T08:00:30Z
instant2: 2022-07-11T08:00:30Z
instant3: 2022-07-15T08:00:30Z
instant1 is before instant2: false
instant1 is before instant3: true

Happy Coding 😊

Java Instant.now() Method with Examples

Java Instant.ofEpochSecond() Method with Examples

Java Instant.ofEpochMilli() Method with Examples

Java Instant.parse() Method with Examples

Java Instant.getEpochSecond() Method with Examples

Java Instant.getNano() Method with Examples

Java Instant.plusSeconds() Method with Examples

Java Instant.plusMillis() Method with Examples

Java Instant.plusNanos() Method with Examples

Java Instant.minusSeconds() Method with Examples

Java Instant.minusMillis() Method with Examples

Java Instant.minusNanos() Method with Examples

Java Instant.atOffset() Method with Examples

Java Instant.atZone() Method with Examples

Java Instant.toEpochMilli() Method with Examples

Java Instant.compareTo() Method with Examples

Java Instant.isAfter() Method with Examples

Java Instant.equals() Method with Examples

Java Instant.toString() Method with Examples

Java Compare two Instant Values