Java Instant.now() Method with Examples
Tags: Java Instant Java 8
In this Java core tutorial we learn how to use the java.time.Instant.now() static method to create a new Instant object which represents the current instant in Java programming language.
Table of contents
Create new Instant object from the System Clock
In Java we can use the Instant.now() static method to instantiate a new Instant object from the system clock as the example Java code below.
InstantNowExample1.java
import java.time.Instant;
public class InstantNowExample1 {
public static void main(String... args) {
Instant instant = Instant.now();
System.out.println(instant);
}
}
2022-05-15T08:13:05.893754600Z
Create new Instant object from the Specified Clock
In Java we can use the Instant.now(Clock clock) static method to instantiate a new Instant object from the specified clock as the example Java code below.
InstantNowExample2.java
import java.time.Clock;
import java.time.Instant;
public class InstantNowExample2 {
public static void main(String... args) {
Clock utcClock = Clock.systemUTC();
Clock systemClock = Clock.systemDefaultZone();
Instant instant1 = Instant.now(utcClock);
Instant instant2 = Instant.now(systemClock);
System.out.println(instant1);
System.out.println(instant2);
}
}
2022-05-15T08:13:45.010757500Z
2022-05-15T08:13:45.010757500Z
Happy Coding 😊
Related Articles
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.isBefore() Method with Examples
Java Instant.equals() Method with Examples