Thursday 5 April 2018

How to use subtractFrom method of Duration class | Java 8 Date and Time


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

DurationDemo.java

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.Temporal;

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

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDateTime = "+localDateTime);

        /*
         * Parameters:
         *
         * temporal - the temporal object to adjust, not null
         *
         * Returns:
         *
         * an object of the same type with the adjustment made, not
         * null
         */

        Temporal temporal = duration.subtractFrom(localDateTime);
        System.out.println("temporal = "+temporal);

    }

}

Output

duration = PT120H
localDateTime = 2018-03-23T09:10:38.480
temporal = 2018-03-18T09:10:38.480

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

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

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