Java Instant Examples



In this source code example, we will show you Java Instant class methods with examples.

Java Instant Examples


import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

public class Main {

    public static void main(String[] args) {

        // get an Instant
        Instant timestamp = Instant.now();
        System.out.println("Timestamp: " + timestamp);

        // String to Instant
        Instant timestampFromString = Instant.parse("2019-02-24T14:31:33.197021300Z");
        System.out.println("\nTimestamp from string: " + timestampFromString);

        // Plus two hours
        Instant twoHourLater = Instant.now().plus(2, ChronoUnit.HOURS);
        System.out.println("\nTwo hours later: " + twoHourLater);

        // Minus 10 minutes
        Instant tenMinutesEarlier = Instant.now().minus(10, ChronoUnit.MINUTES);
        System.out.println("Ten minutes earlier: " + tenMinutesEarlier);

        // check if one Instant is after another Instant
        Instant timestamp1 = Instant.now();
        Instant timestamp2 = timestamp1.plusSeconds(10);
        boolean isAfter = timestamp1.isAfter(timestamp2);
        System.out.println("\n" + timestamp1 + " is " + (isAfter ? "" : "not ") + "after " + timestamp2);

        // check if one Instant is before another Instant
        boolean isBefore = timestamp1.isBefore(timestamp2);
        System.out.println(timestamp1 + " is " + (isBefore ? "" : "not ") + "before " + timestamp2);

        // difference between two Instants
        long difference = timestamp1.until(timestamp2, ChronoUnit.SECONDS);
        System.out.println("Between " + timestamp1 + " and " + timestamp2 + " there are " + difference + " seconds");

        // convert Instant to LocalDateTime
        LocalDateTime ldt = LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC);
        System.out.println("\nInstant to LocaleDateTime: " + ldt);
        // convert LocalDateTime to Instant
        Instant instantLDT = LocalDateTime.now().toInstant(ZoneOffset.UTC);
        System.out.println("LocaleDateTime to Instant: " + instantLDT);

        // convert Instant to ZonedDateTime
        ZonedDateTime zdt = Instant.now().atZone(ZoneId.of("Europe/Paris"));
        System.out.println("Instant to ZonedDateTime: " + zdt + " (offset: " + zdt.getOffset() + ")");
        // convert ZonedDateTime to Instant
        Instant instantZDT = LocalDateTime.now().atZone(ZoneId.of("Europe/Paris")).toInstant();
        System.out.println("ZonedDateTime to Instant: " + instantZDT);

        // convert Instant to OffsetDateTime
        OffsetDateTime odt = Instant.now().atOffset(ZoneOffset.of("+02:00"));
        System.out.println("Instant to OffsetDateTime: " + odt + " (offset: " + odt.getOffset() + ")");
        // convert OffsetDateTime to Instnat
        Instant instantODT = LocalDateTime.now().atOffset(ZoneOffset.of("+02:00")).toInstant();
        System.out.println("OffsetDateTime to Instant: " + instantODT);
    }
}


Output:

Timestamp: 2021-11-10T12:49:11.236292200Z

Timestamp from string: 2019-02-24T14:31:33.197021300Z

Two hours later: 2021-11-10T14:49:11.304788800Z
Ten minutes earlier: 2021-11-10T12:39:11.304788800Z

2021-11-10T12:49:11.304788800Z is not after 2021-11-10T12:49:21.304788800Z
2021-11-10T12:49:11.304788800Z is before 2021-11-10T12:49:21.304788800Z
Between 2021-11-10T12:49:11.304788800Z and 2021-11-10T12:49:21.304788800Z there are 10 seconds

Instant to LocaleDateTime: 2021-11-10T12:49:11.305785800
LocaleDateTime to Instant: 2021-11-10T18:19:11.338699600Z
Instant to ZonedDateTime: 2021-11-10T13:49:11.338699600+01:00[Europe/Paris] (offset: +01:00)
ZonedDateTime to Instant: 2021-11-10T17:19:11.347674900Z
Instant to OffsetDateTime: 2021-11-10T14:49:11.356655100+02:00 (offset: +02:00)
OffsetDateTime to Instant: 2021-11-10T16:19:11.357650400Z

Free Spring Boot Tutorial - 5 Hours Full Course


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

Comments