In this article, we will learn how to get instantaneous moment at GMT/UTC using Instant.now() method provided in Java 1.8 version
Get instantaneous moment at GMT/UTC from Clock :
- There are 2 ways to get Instant at GMT/UTC –
- Instant.now() – gets the current instant at the GMT/UTC from the system clock
- Instant.now(Clock) – gets the current instant at the GMT/UTC from the specified clock
- Lets see an example to get current instant from system & specified clocks
GetUtcInstantFromClock.java
package in.bench.resources.java8.instant.examples;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
public class GetUtcInstantFromClock {
public static void main(String[] args) {
// default zone
ZoneId zoneId = ZoneId.systemDefault();
System.out.println("Default System Zone is = "
+ zoneId);
// 1. get an instantaneous moment at GMT/UTC
Instant instant = Instant.now();
System.out.println("\nCurrent Instant at UTC/GMT using Instant.now() is = "
+ instant);
// 2. get an instantaneous moment at GMT/UTC passing Clock
Clock clock = Clock.system(ZoneId.of("Europe/Paris"));
Instant instant2 = Instant.now(clock);
System.out.print("\nCurrent Instant at UTC/GMT using Instant & Clock is = "
+ instant2);
}
}
Output:
Default System Zone is = Asia/Calcutta
Current Instant at UTC/GMT using Instant.now() is = 2022-08-19T14:37:16.881925100Z
Current Instant at UTC/GMT using Instant & Clock is = 2022-08-19T14:37:16.898933Z
Related Articles:
- Java 8 – Instant with method details and examples
- Java 8 – How to get instantaneous moment at UTC/GMT using Instant ?
- Java 8 – How to form an Instant passing Seconds and Nanoseconds fields ?
- Java 8 – How to get Seconds and Nanoseconds from an Instant ?
- Java 8 – How to parse Instant in String form ?
- Java 8 – How to convert Instant to LocalDate ?
- Java 8 – How to convert Instant to LocalTime ?
- Java 8 – How to convert Instant to LocalDateTime ?
- Java 8 – How to convert Instant to ZonedDateTime ?
- Java 8 – How to convert Instant to an OffsetDateTime ?
- Java 8 – How to convert Instant to number of Seconds and vice-versa ?
- Java 8 – How to convert Instant to number of Milliseconds and vice-versa ?
- Java 8 – How to convert Instant to java.util.Date and vice-versa ?
- Java 8 – How to convert Instant to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert Instant to Calendar and vice-versa ?
- Java 8 – How to convert Instant to GregorianCalendar and vice-versa ?
- Java 8 – How to convert Instant to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to an Instant in different ways ?
- Java 8 – How to add Second, Millisecond and Nanosecond to an Instant ?
- Java 8 – How to subtract Second, Millisecond and Nanosecond from an Instant ?
- Java 8 – How to check whether an Instant is Before another Instant ?
- Java 8 – How to check whether an Instant is After another Instant ?
- Java 8 – How to compare two Instant instances ?
- Java 8 – How to find difference between two Instant instances using Duration ?
- Java 9 – Find difference between two Instant instances upto nanosecond precision ?
- Java 9 – How to convert Instant to LocalDate using ofInstant() method ?
- Java 9 – How to convert Instant to LocalTime using ofInstant() method ?
- More Java 8 Date/Time API examples
References:
Happy Coding !!
Happy Learning !!