Tuesday, 3 April 2018

How to subtract days, hours etc using minus methods of Duration class | Java 8 Date and Time


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

DurationDemo1.java

import java.time.Duration;

public class DurationDemo1
{
    public static void main(String[] args)
    {
        Duration duration = Duration.ofDays(2);
        System.out.println("duration     = "+duration);

        duration = duration.minusDays(1);
        System.out.println("Minus 1 day   = "+duration);
       
        duration = duration.minusHours(2);
        System.out.println("Minus 2 hours = "+duration);
       
        duration = duration.minusMinutes(50);
        System.out.println("Minus 50 mins = "+duration);
       
        duration = duration.minusSeconds(40);
        System.out.println("Minus 40 secs = "+duration);
       
        duration = duration.minusMillis(10);
        System.out.println("Minus 10 Millis = "+duration);
       
        duration = duration.minusNanos(70000);
        System.out.println("Minus 70000 Nanos = "+duration);

    }

}

Output

duration     = PT48H
Minus 1 day   = PT24H
Minus 2 hours = PT22H
Minus 50 mins = PT21H10M
Minus 40 secs = PT21H9M20S
Minus 10 Millis = PT21H9M19.99S
Minus 70000 Nanos = PT21H9M19.98993S

DurationDemo2.java

import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class DurationDemo2
{
    public static void main(String[] args)
    {
        Duration duration = Duration.ofDays(2);
        System.out.println("duration         = " + duration);

        /*
         * Parameters:
         *
         * amountToAdd - the amount to add, measured in terms of the
         * unit, positive or negative unit - the unit that the amount
         * is measured in, must have an exact duration, not null
         *
         * Returns:
         *
         * a Duration based on this duration with the specified
         * duration added, not null
         */

        duration = duration.minus(1, ChronoUnit.DAYS);
        System.out.println("Minus 1 days      = " + duration);

        duration = duration.minus(20, ChronoUnit.MINUTES);
        System.out.println("Minus 20 minutes  = " + duration);

    }

}

Output

duration         = PT48H
Minus 1 days      = PT24H
Minus 20 minutes  = PT23H40M

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/c7899e666996cf29a48ef15f0030404863025004/BasicJava_2018/DurationDemo_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