In this article, we will learn how to get Seconds & Nanoseconds from an Instant using getEpochSecond() & getNano() methods provided in Java 1.8 version
Get Seconds/Nanos from an Instant :
- There are 2 methods available to get Seconds/Nanos from an Instant –
- getEpochSecond() – gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z
- getNano() – gets the number of nanoseconds, later along the time-line, from the start of the second
- Lets see an example to get Seconds & Nanoseconds from an Instant
GetSecondAndNanoSecondFromAnInstant.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
public class GetSecondAndNanoSecondFromAnInstant {
public static void main(String[] args) {
// get current Date/time or Instant at UTC/GMT
Instant instant = Instant.now();
System.out.println("Current Instant at UTC/GMT is :- \n"
+ instant);
// 1. get Seconds from an Instant
long seconds = instant.getEpochSecond();
System.out.println("\nInstant to number of Seconds in long format :- \n"
+ seconds);
// 2. get Nanoseconds from an Instant
int nanoseconds = instant.getNano();
System.out.print("\nFractional Nanoseconds part from an Instant in integer format :- \n"
+ nanoseconds);
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-19T12:36:00.991068300Z
Instant to number of Seconds in long format :-
1660912560
Fractional Nanoseconds part from an Instant in integer format :-
991068300
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 !!