JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Java 8 Date Time API – Instant Example

April 11, 2015 by Krishna Srinivasan Leave a Comment

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

[code lang=”java”]
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);
}
}
[/code]

Output for the above program will be:

[code]
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
[/code]

Filed Under: Java Tagged With: Java 8

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved