Thursday 18 January 2018

How to get the current time using now methods of Java 8 LocalTime Class | Java 8 Date and Time


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

LocalTimeDemo1.java

import java.time.Clock;
import java.time.LocalTime;

public class LocalTimeDemo1
{

    public static void main(String[] args)
    {
        Clock clock = Clock.systemUTC();

        /*
         * Parameters:
         *
         * clock - the clock to use, not null
         *
         * Returns:
         *
         * the current time, not null
         */

        LocalTime time = LocalTime.now(clock);
        System.out.println(time);
    }

}

Output

04:59:41.320

LocalTimeDemo2.java

import java.time.LocalTime;
import java.time.ZoneId;

public class LocalTimeDemo2
{

    public static void main(String[] args)
    {
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);

        /*
         * Parameters:
         *
         * zone - the zone ID to use, not null
         *
         * Returns:
         *
         * the current time using the system clock, not null
         */

        LocalTime time = LocalTime.now(zoneId);
        System.out.println(time);
    }

}

Output 

Asia/Calcutta
10:29:51.536

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/0f51b390145d58b657670d4890d43cef82a5202a/BasicJava/LocalTimeDemo_now_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
  • No comments:

    Post a Comment