Wednesday 4 April 2018

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


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

DurationDemo.java

import java.time.Duration;
import java.time.LocalTime;

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

        /*
         * Parameters:
         *
         * nanoOfSecond - the nano-of-second to represent, from 0 to
         * 999,999,999
         *
         * Returns:
         *
         * a Duration based on this period with the requested
         * nano-of-second, not null
         */

        duration = duration.withNanos(10000);
        System.out.println("After withNanos = " + duration.getNano());

        System.out.println("Before withSeconds = " + duration.getSeconds());

        /*
         * Parameters:
         *
         * seconds - the seconds to represent, may be negative
         *
         * Returns:
         *
         * a Duration based on this period with the requested seconds,
         * not null
         */

        duration = duration.withSeconds(3000);
        System.out.println("After withSeconds = " + duration.getSeconds());

    }

}

Output

Before withNanos = 0
After withNanos = 10000
Before withSeconds = 43200
After withSeconds = 3000

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

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

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