Thursday, 5 April 2018

How to use get methods of Instant Class | Java 8 Date and Time


Click here to watch on Youtube:
https://www.youtube.com/watch?v=nyoB5z-8Mak&list=UUhwKlOVR041tngjerWxVccw

InstantDemo1.java

import java.time.Instant;

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

        Instant instant = Instant.now();
        System.out.println("instant = "+instant);

        /*
         * Returns:the seconds from the epoch of 1970-01-01T00:00:00Z
         */

        long secondValue = instant.getEpochSecond();
        System.out.println("secondValue = "+secondValue);

        /*
         * Returns:the nanoseconds within the second, always positive,
         * never exceeds 999,999,999
         */

        long nanoValue = instant.getNano();
        System.out.println("nanoValue   = "+nanoValue);
       
    }

}

Output

instant = 2018-03-24T04:12:53.409Z
secondValue = 1521864773
nanoValue   = 409000000

InstantDemo2.java

import java.time.Instant;
import java.time.temporal.ChronoField;

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

        Instant instant = Instant.now();
        System.out.println("instant = "+instant);

        /*
         * Parameters:
         *
         * field - the field to get, not null
         *
         * Returns:
         *
         * the value for the field
         */

        long nanoSecondValue = instant.getLong(ChronoField.NANO_OF_SECOND);
        System.out.println("nanoSecondValue = "+nanoSecondValue);
       
    }

}

Output

instant = 2018-03-24T04:13:03.625Z
nanoSecondValue = 625000000

InstantDemo3.java

import java.time.Instant;
import java.time.temporal.ChronoField;

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

        Instant instant = Instant.now();
        System.out.println("instant = "+instant);

        /*
         * Parameters:
         *
         * field - the field to get, not null
         *
         * Returns:
         *
         * the value for the field
         */

        int nanoSecondValue = instant.get(ChronoField.NANO_OF_SECOND);
        System.out.println("nanoSecondValue = "+nanoSecondValue);
       
    }

}

Output

instant = 2018-03-24T04:13:14.609Z
nanoSecondValue = 609000000

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/cd3c99ca353758a9764fe433bd563318d1244d3f/BasicJava_2018/InstantDemo_get_methods/?at=master

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

    Post a Comment