Tuesday 10 April 2018

How to add sec, millis etc using plus method of Instant class | Java 8 Date and Time


InstantDemo1.java

import java.time.Instant;

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

        Instant instant = Instant.parse("2017-12-03T10:15:30.00Z");
        System.out.println("Before plus         = "+instant);
       
        /*
         * Parameters:
         *
         * secondsToAdd - the seconds to add, positive or negative
         *
         * Returns:
         *
         * an Instant based on this instant with the specified seconds
         * added, not null
         */

        instant = instant.plusSeconds(10000);
        System.out.println("After  plusSeconds  = "+instant);
       
        /*
         * Parameters:
         *
         * millisToAdd - the milliseconds to add, positive or negative
         *
         * Returns:
         *
         * an Instant based on this instant with the specified
         * milliseconds added, not null
         */

        instant = instant.plusMillis(20000);
        System.out.println("After  plusMillis   = "+instant);
       
        /*
         * Parameters:
         *
         * nanosToAdd - the nanoseconds to add, positive or negative
         *
         * Returns:
         *
         * an Instant based on this instant with the specified
         * nanoseconds added, not null
         */

        instant = instant.plusNanos(50000);
        System.out.println("After  plusNanos    = "+instant);

    }

}

Output

Before plus         = 2017-12-03T10:15:30Z
After  plusSeconds  = 2017-12-03T13:02:10Z
After  plusMillis   = 2017-12-03T13:02:30Z
After  plusNanos    = 2017-12-03T13:02:30.000050Z

InstantDemo2.java

import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class InstantDemo2
{
    public static void main(String[] args)
    {
        Instant instant = Instant.parse("2017-12-03T10:15:30.00Z");
        System.out.println("Before plus = "+instant);
       
        /*
         * Parameters:
         *
         * amountToAdd - the amount of the unit to add to the result,
         * may be negative
         *
         * unit - the unit of the amount to add, not null
         *
         * Returns:
         *
         * an Instant based on this instant with the specified amount
         * added, not null
         */

        instant = instant.plus(10,ChronoUnit.DAYS);
        System.out.println("After  plus = "+instant);
    }

}

Output

Before plus = 2017-12-03T10:15:30Z
After  plus = 2017-12-13T10:15:30Z

InstantDemo3.java

import java.time.Duration;
import java.time.Instant;

public class InstantDemo3
{
    public static void main(String[] args)
    {
        Instant instant = Instant.parse("2017-12-03T10:15:30.00Z");
        System.out.println("Before plus = "+instant);
       
        Duration duration = Duration.ofDays(5);
        /*
         * Parameters:
         *
         * amountToAdd - the amount to add, not null
         *
         * Returns:
         *
         * an Instant based on this instant with the addition made,
         * not null
         */

        instant = instant.plus(duration);
        System.out.println("After  plus = "+instant);
    }

}

Output

Before plus = 2017-12-03T10:15:30Z
After  plus = 2017-12-08T10:15:30Z

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/6b375c8df41faf1944948958eab5259beb7895e3/BasicJava_2018/InstantDemo_plus/?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