Wednesday, 28 February 2018

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


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

ClockDemo.java

import java.time.Clock;
import java.time.ZoneId;

public class ClockDemo
{

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

        /*
         * Parameters:
         *
         * zone - the time-zone to change to, not null
         *
         * Returns:
         *
         * a clock based on this clock with the specified time-zone,
         * not null
         */

        Clock clock2 = clock1.withZone(ZoneId.systemDefault());
        System.out.println("clock2 = " + clock2.instant());
    }

}

Output

clock1 = 2018-02-13T04:24:13.638Z
clock2 = 2018-02-13T04:24:32.123Z

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/f479c67d9d2a797db17daef3132e32f3fda78b25/BasicJava/ClockDemo_withzone/?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