Monday 21 May 2018

How to use ofEpoch methods of Instant class | Java 8 Date and Time


Click here to watch on Youtube: 
https://www.youtube.com/watch?v=Lp-O6Ir99jQ&index=109&list=UUhwKlOVR041tngjerWxVccw

InstantDemo1.java

import java.time.Instant;

public class InstantDemo1
{
    public static void main(String[] args)
    {

        /*
         * Obtains an instance of Instant using milliseconds from the
         * epoch of 1970-01-01T00:00:00Z.
         *
         * Parameters:
         *
         * epochMilli - the number of milliseconds from
         * 1970-01-01T00:00:00Z
         *
         * Returns:
         *
         * an instant, not null
         */

        Instant instant = Instant.ofEpochMilli(50000L);
        System.out.println(instant);
    }

}

Output

1970-01-01T00:00:50Z

InstantDemo2.java

import java.time.Instant;

public class InstantDemo2
{
    public static void main(String[] args)
    {
        /*
         * Obtains an instance of Instant using seconds from the epoch
         * of 1970-01-01T00:00:00Z.
         *
         * Parameters:
         *
         * epochSecond - the number of seconds from
         * 1970-01-01T00:00:00Z
         *
         * Returns:
         *
         * an instant, not null
         */

        Instant instant = Instant.ofEpochSecond(100000L);
        System.out.println(instant);
    }

}

Output

1970-01-02T03:46:40Z

InstantDemo3.java

import java.time.Instant;

public class InstantDemo3
{
    public static void main(String[] args)
    {
        /*
         * Obtains an instance of Instant using seconds from the epoch
         * of 1970-01-01T00:00:00Z and nanosecond fraction of second.
         *
         * Parameters:
         *
         * epochSecond - the number of seconds from
         * 1970-01-01T00:00:00Z
         *
         * nanoAdjustment - the nanosecond adjustment to the number of
         * seconds, positive or negative
         *
         * Returns:
         *
         * an instant, not null
         */

        Instant instant = Instant.ofEpochSecond(90900L, 8000);
        System.out.println(instant);
    }

}

Output

1970-01-02T01:15:00.000008Z

Click the below link to download the code:
https://sites.google.com/site/ramj2eev2/java_basics/InstantDemo_ofEpoch.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava_2018/InstantDemo_ofEpoch

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/80c7c27095bd0aa4eb0109eac12d92549dcd717d/BasicJava_2018/InstantDemo_ofEpoch/?at=master

See also:

  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment