Wednesday 21 February 2018

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


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

ZoneDateTimeDemo.java

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

public class ZoneDateTimeDemo
{

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

        /*
         * Obtains the current date-time from the specified clock.
         *
         * Parameters:
         *
         * clock - the clock to use, not null
         *
         * Returns:
         *
         * the current date-time, not null
         */

        ZonedDateTime zonedDateTime1 = ZonedDateTime.now(clock);
        System.out.println(zonedDateTime1);

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

        /*
         * Obtains the current date-time from the system clock in the
         * specified time-zone.
         *
         * Parameters:
         *
         * zone - the zone ID to use, not null
         *
         * Returns:
         *
         * the current date-time using the system clock, not null
         *
         */

        ZonedDateTime zonedDateTime2 = ZonedDateTime.now(zoneId);
        System.out.println(zonedDateTime2);
    }

}

Output

SystemClock[Z]
2018-02-08T04:30:10.162Z
Asia/Calcutta
2018-02-08T10:02:26.163+05:30[Asia/Calcutta]

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/914905cc2357de2bddb749208190c8f0bf0f9978/BasicJava/ZoneDateTimeDemo_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