In this article, we will learn how to convert Instant to number of Milliseconds and vice-versa using toEpochMilli() and ofEpochMilli() methods of Instant respectively provided in Java 1.8 version
Conversion of Instant to Millisecond & vice-versa :
There are 2 methods available in an Instant for conversion of Instant to Millisecond and vice-versa,
- toEpochMilli() – converts invoking instant to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z
- ofEpochMilli(epochMilli) – gets an instance of Instant using milliseconds from the epoch of 1970-01-01T00:00:00Z
1. Convert Instant to Milliseconds :
- toEpochMilli() – Converts an Instant to number of Milliseconds from 1970-01-01T00:00:00Z in long type
ConvertInstantToMilliSeconds.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
public class ConvertInstantToMilliSeconds {
public static void main(String[] args) {
// 1. get current Date/time or Instant at UTC
Instant instant = Instant.now();
System.out.println("Current Instant at UTC/GMT is :- \n"
+ instant);
// 2. get Milliseconds from an Instant
long milliseconds = instant.toEpochMilli();
System.out.print("\nInstant to number of Milliseconds in long type :- \n"
+ milliseconds);
}
}
Output:
Current Instant at UTC/GMT is :-
2022-08-19T17:59:12.020844500Z
Instant to number of Milliseconds in long type :-
1660931952020
2. Convert Seconds to Instant :
- ofEpochMilli(long) – This method obtains an instance of
Instant
from a Milliseconds value provided as argument in long type
ConvertMilliSecondsToInstant.java
package in.bench.resources.java8.instant.examples;
import java.time.Instant;
public class ConvertMilliSecondsToInstant {
public static void main(String[] args) {
// 1. milliseconds in long format
long milliseconds = 1660931952020L;
System.out.println("Number of Milliseconds in long type :- \n"
+ milliseconds);
// 2. convert milliseconds to an Instant
Instant instant = Instant.ofEpochMilli(milliseconds);
System.out.print("\nMilliseconds to an Instant is :- \n"
+ instant);
}
}
Output:
Number of Milliseconds in long type :-
1660931952020
Milliseconds to an Instant is :-
2022-08-19T17:59:12.020Z
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 !!