Wednesday 23 May 2018

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


Click here to watch on Youtube:
https://www.youtube.com/watch?v=fsxx_H_BIuQ&list=UUhwKlOVR041tngjerWxVccw

InstantDemo1.java

import java.time.Instant;

public class InstantDemo1
{
    public static void main(String[] args)
    {
        Instant instant1 = Instant.parse("2015-12-03T10:15:30.00Z");
        System.out.println("instant1 = "+instant1);
       
        Instant instant2 = Instant.now();
        System.out.println("instant2 = "+instant2);
       
        /*
         * Parameters:
         *
         * adjuster - the adjuster to use, not null
         *
         * Returns:
         *
         * an Instant based on this with the adjustment made, not null
         */

        Instant instant3 = instant1.with(instant2);
        System.out.println("instant3 = "+instant3);
    }

}

Output

instant1 = 2015-12-03T10:15:30Z
instant2 = 2018-04-08T04:36:01.776Z
instant3 = 2018-04-08T04:36:01.776Z

InstantDemo2.java

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

public class InstantDemo2
{
    public static void main(String[] args)
    {
          Instant instant1 = Instant.parse("2017-12-03T10:15:30.00Z");
          System.out.println("instant1 = "+instant1);
         
        /*
         * Parameters:
         *
         * field - the field to set in the result, not null
         *
         * newValue - the new value of the field in the result
         *
         * Returns:
         *
         * an Instant based on this with the specified field set, not
         * null
         */

          Instant instant2 = instant1.with(ChronoField.NANO_OF_SECOND, 800);
          System.out.println("instant2 = "+instant2);
    }

}


Output

instant1 = 2017-12-03T10:15:30Z
instant2 = 2017-12-03T10:15:30.000000800Z

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/80c7c27095bd0aa4eb0109eac12d92549dcd717d/BasicJava_2018/InstantDemo_with/?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