Tuesday 3 April 2018

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


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

DurationDemo.java

import java.time.Duration;

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


        /*
         * Returns:the number of days in the duration, may be negative
         */

        System.out.println("toDays    = " + duration.toDays());

        /*
         * Returns:the number of hours in the duration, may be
         * negative
         */

        System.out.println("toHours   = " + duration.toHours());

        /*
         * Returns:the number of minutes in the duration, may be
         * negative
         */

        System.out.println("toMinutes = " + duration.toMinutes());

        /*
         * Returns:the total length of the duration in milliseconds
         */

        System.out.println("toMillis  = " + duration.toMillis());

        /*
         * Returns:the total length of the duration in nanoseconds
         */

        System.out.println("toNanos   = " + duration.toNanos());

    }

}

Output

duration    = PT48H
toDays    = 2
toHours   = 48
toMinutes = 2880
toMillis  = 172800000
toNanos   = 172800000000000

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

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

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