Tuesday 27 February 2018

Java 8 Clock Class Introduction | Java 8 Date and Time | Java Date and Time


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

Click the below Image to Enlarge
Java 8 Clock Class Introduction | Java 8 Date and Time | Java Date and Time
ClockDemo1.java

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

public class ClockDemo1
{

    public static void main(String[] args)
    {
        /*
         * Returns:
         *
         * a clock that uses the best available system clock in the
         * default zone, not null
         */

        Clock clock = Clock.systemDefaultZone();
        System.out.println("clock = "+clock);

        /*
         * Returns:
         *
         * the time-zone being used to interpret instants, not null
         */

        ZoneId zoneId = clock.getZone();
        System.out.println("zoneId = "+zoneId);
    }

}

Output

clock = SystemClock[Asia/Calcutta]
zoneId = Asia/Calcutta

ClockDemo2.java

import java.time.Clock;
import java.time.Instant;

public class ClockDemo2
{

    public static void main(String[] args)
    {
        /*
         * Returns:
         *
         * a clock that uses the best available system clock in the
         * UTC zone, not null
         */

        Clock clock = Clock.systemUTC();
        System.out.println("clock = "+clock);
        /*
         * Returns:
         *
         * the current instant from this clock, not null
         */

        Instant instant = clock.instant();     
        System.out.println("instant = "+instant);
    }

}

Output

clock = SystemClock[Z]
instant = 2018-02-12T04:17:39.294Z

ClockDemo3.java

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

public class ClockDemo3
{

    public static void main(String[] args)
    {

        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println("zoneId = " + zoneId);

        /*
         * Parameters:
         *
         * zone - the time-zone to use to convert the instant to
         * date-time, not null
         *
         * Returns:
         *
         * a clock that uses the best available system clock in the
         * specified zone, not null
         */

        Clock clock = Clock.system(zoneId);
        System.out.println("Clock = " + clock);
    }

}

Output

zoneId = Asia/Calcutta
Clock = SystemClock[Asia/Calcutta]

Refer: 
https://docs.oracle.com/javase/8/docs/api/index.html?java/time/Clock.html

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

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

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

    1. This is really nice to read the content of this blog. it is a very extensive and vast knowledgeable platform that has been given by this blog. Thanks for sharing this helpful blog. Time Clock

      ReplyDelete