In this article, we will learn how to form an Instant passing Seconds & Nanoseconds using Instant.ofEpochSecond() method provided in Java 1.8 version
Form an Instant passing Seconds/Nanos :
- There are 2 methods available to form an Instant passing Seconds/Nanos –
- ofEpochSecond(epochSecond) –
- gets an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z
- ofEpochSecond(epochSecond, nanoAdjustment) –
- gets an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second
- ofEpochSecond(epochSecond) –
- Lets see an example to form an instant passing Seconds & Nanoseconds
FormUtcInstantUsingSecondAndNano.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
public class FormUtcInstantUsingSecondAndNano {
public static void main(String[] args) {
// 1. form an Instant passing Second/Nanos
Instant instant1 = Instant.ofEpochSecond(1660818209, 443097123);
System.out.println("Instant using Seconds/Nanos is = " + instant1);
// 2. form an Instant passing only Seconds
Instant instant2 = Instant.ofEpochSecond(1660818209);
System.out.print("\nInstant using Seconds only is = " + instant2);
}
}
Output:
Instant using Seconds/Nanos is = 2022-08-18T10:23:29.443097123Z
Instant using Seconds only is = 2022-08-18T10:23:29Z
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 !!