Wednesday, 17 January 2018

How to get the current time using now method which accepts zoneId | Java 8 LocalTime Class


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

LocalTimeDemo.java

import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;

public class LocalTimeDemo
{

    public static void main(String[] args)
    {
        ZoneId zoneId1 = ZoneId.of("Asia/Kolkata");

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

        LocalTime indiaTime = LocalTime.now(zoneId1);
        System.out.println("indiaTime   = " + indiaTime);

        ZoneId zoneId2 = ZoneId.of("Asia/Bangkok");
        LocalTime bangkokTime = LocalTime.now(zoneId2);
        System.out.println("bangkokTime = " + bangkokTime);

        long hours = ChronoUnit.HOURS.between(indiaTime, bangkokTime);
        System.out.println("\nHours between two Time Zone =  " + hours);

        long minutes = ChronoUnit.MINUTES.between(indiaTime, bangkokTime);
        System.out.println("Minutes between two time zone =  " + minutes);
    }

}

Output

indiaTime   = 09:22:58.690
bangkokTime = 10:53:24.641

Hours between two Time Zone =  1
Minutes between two time zone =  90

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

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

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