Monday 2 April 2018

How to create duration object using of methods of Duration class | Java 8 Date and Time


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

DurationDemo1.java

import java.time.Duration;

public class DurationDemo1
{
    public static void main(String[] args)
    {
        /*
         * Parameters:
         *
         * days - the number of days, positive or negative
         *
         * Returns:
         *
         * a Duration, not null
         */

        Duration duration1 = Duration.ofDays(10);
        System.out.println("duration1 = "+duration1);
       
        /*
         * Parameters:
         *
         * hours - the number of hours, positive or negative
         *
         * Returns:
         *
         * a Duration, not null
         */

        Duration duration2 = Duration.ofHours(10);
        System.out.println("duration2 = "+duration2);
    }

}

Output

duration1 = PT240H
duration2 = PT10H

DurationDemo2.java

import java.time.Duration;

public class DurationDemo2
{
    public static void main(String[] args)
    {
        /*
         * Parameters:
         *
         * millis - the number of milliseconds, positive or negative
         *
         * Returns:
         *
         * a Duration, not null
         */

        Duration duration1 = Duration.ofMillis(1000);
        System.out.println("duration1 = "+duration1);
       
        /*
         * Parameters:
         *
         * minutes - the number of minutes, positive or negative
         *
         * Returns:
         *
         * a Duration, not null
         */

        Duration duration2 = Duration.ofMinutes(20);
        System.out.println("duration2 = "+duration2);
    }

}

Output

duration1 = PT1S
duration2 = PT20M

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

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

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