Tuesday, 27 February 2018

How to use offset method of Clock class | Java 8 Date and Time


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

ClockDemo.java

import java.time.Clock;
import java.time.Duration;

public class ClockDemo
{

    public static void main(String[] args)
    {
        Clock baseClock = Clock.systemUTC();
        System.out.println("instant of baseClock = " + baseClock.instant());

        Duration duration = Duration.ofHours(6);
        System.out.println("duration = " + duration);

        /*
         * Parameters:
         *
         * baseClock - the base clock to add the duration to, not null
         * offset
         *
         * Duration - the duration to add, not null
         *
         * Returns:
         *
         * a clock based on the base clock with the duration added,
         * not null
         */


        Clock clock = Clock.offset(baseClock, duration);
        System.out.println("instant of clock     = " + clock.instant());
    }

}

Output

instant of baseClock = 2018-02-13T03:22:38.254Z
duration = PT6H
instant of clock     = 2018-02-13T09:23:36.572Z

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ClockDemo_offset_duration.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ClockDemo_offset_duration

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/1e79a72b731c44149257d4c3aece211de3cbaa86/BasicJava/ClockDemo_offset_duration/?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
  • No comments:

    Post a Comment