Tuesday, 10 April 2018

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


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

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 minus         = "+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.minusSeconds(10000);
        System.out.println("After  minusSeconds  = "+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.minusMillis(20000);
        System.out.println("After  minusMillis   = "+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.minusNanos(50000);
        System.out.println("After  minusNanos    = "+instant);

    }

}

Output

Before minus         = 2017-12-03T10:15:30Z
After  minusSeconds  = 2017-12-03T07:28:50Z
After  minusMillis   = 2017-12-03T07:28:30Z
After  minusNanos    = 2017-12-03T07:28:29.999950Z

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-23T10:15:30.00Z");
        System.out.println("Before minus = "+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.minus(10,ChronoUnit.DAYS);
        System.out.println("After  minus = "+instant);
    }

}

Output

Before minus = 2017-12-23T10:15:30Z
After  minus = 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-15T10: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.minus(duration);
        System.out.println("After  minus = "+instant);
    }

}

Output

Before plus  = 2017-12-15T10:15:30Z
After  minus = 2017-12-10T10:15:30Z

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

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

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