Along with lambda expression, virtual methods, streams and many other nice features, Java 8 has also updated a new Date and Time API which is maintained under the JSR-310. One of the greatest on this new API is that all the date and time related APIs are unified under the same package java.time. In my previous articles I have explained about the LocalDate and LocalTime .This example demonstrates how to use one of the new class java.time.Instant defined under that date and time package.
If you have any clarifications, please write it in the comments section or post it in our facebook page. You can read more articles on Java 8 here.
Instant
Human read the dates as year, month and days. As this is the easiest format for human brain to understand and remembered well. But, machines require a single lengthy string for a specific instant of time to be stored. The purpose of the Instant class is returning a single number for that particular instant of time. It is point in time with nanoseconds precision. Instant class is similar to the previous java.util.Date class except that formar uses the milliseconds precision.
A value returned from the Instant class always counts time beginning from the first second of January 1, 1970 (1970-01-01T00:00:00Z). Any time requested before this date will have a negative value. In simple words, this Instance class calculates any date/time with reference to January 1, 1970 (1970-01-01T00:00:00Z) and stores it as the miliseconds. For example, if you are requesting to get the EPOCH of second “1”, it would calculate the first second from the above date and would return 1970-01-01T00:00:01Z.
This class defines several methods that are useful for fetching the values from the stored object. This example represents few important methods are useful for the programmers in normal scenarios.
- Class Name : Instant
- Package : java.time
- Version Released : Java 1.8
Instant – Few Methods
- now() – This static method gets the current Instant time from the default system clock
- ofEpochSecond(long epochSecond) – This static method returns the Instant time using the specified epochSecond value. Note that whatever value you have passed, it starts the calculation from January 1, 1970 (1970-01-01T00:00:00Z)
- toEpochMilli() – This instance methods converts this instant object to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z.
- ofEpochMilli(long epochMilli) – This static method obtains an instance of Instant using milliseconds from the epoch of 1970-01-01T00:00:00Z.
- isAfter(Instant otherInstant) – This instance method checks if the current Instant object value is after the specified Instant value
Instant – Example
package javabeat.net; import java.time.Instant; /** * Instant Example * * @author (www.javabeat.net) * */ public class InstantExample { public static void main(String[] args) { Instant instant = Instant.now(); Instant instantOfEpochSec = Instant.ofEpochSecond(1); long epoch = instant.toEpochMilli(); Instant instantOfEpochMili = Instant.ofEpochMilli(epoch); boolean isAfter = instant.isAfter(instantOfEpochSec); System.out.println("LocalTime Example Demo"); System.out.println("----------------------"); System.out.println("Current Time : " + instant.toEpochMilli()); System.out.println("Current Time from EPOCH : " + instantOfEpochSec); System.out.println("EPOCH MiliSeconds : " + epoch); System.out.println("Instant From EPOCH Miliseconds : " + instantOfEpochMili); System.out.println("Instant().isAfter() : " + isAfter); } }
Output for the above program will be:
Instant Example Demo ---------------------- Current Time : 1428758907415 Current Time from EPOCH : 1970-01-01T00:00:01Z EPOCH MiliSeconds : 1428758907415 Instant From EPOCH Miliseconds : 2015-04-11T13:28:27.415Z Instant().isAfter() : true