Wednesday 4 April 2018

How to use get method of Duration class which accepts Temporal unit | Java 8 Date and Time


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

DurationDemo.java

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

public class DurationDemo
{
    public static void main(String[] args)
    {
        Duration duration = Duration.between(LocalTime.MIDNIGHT,
                LocalTime.NOON);
        System.out.println("duration = " + duration);

        /*
         * Gets the value of the requested unit.
         *
         * Parameters:
         *
         * unit - the TemporalUnit for which to return the value
         *
         * Returns:
         *
         * the long value of the unit
         *
         */

        long secondsValue = duration.get(ChronoUnit.SECONDS);
        System.out.println("secondsValue = " + secondsValue);

        long nanosValue = duration.get(ChronoUnit.NANOS);
        System.out.println("nanosValue = " + nanosValue);
    }

}

Output

duration = PT12H
secondsValue = 43200
nanosValue = 0

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

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

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