Tuesday 27 February 2018

How to use ofLocal, ofStrict methods of ZonedDateTime Class | Java 8 Date and Time


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

ZoneDateTimeDemo1.java

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class ZoneDateTimeDemo1
{

    public static void main(String[] args)
    {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

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

        ZoneOffset zoneOffSet = ZoneOffset.UTC;
        System.out.println(zoneOffSet);

        /*
         * Parameters:
         *
         * localDateTime - the local date-time, not null
         *
         * zone - the time-zone, not null
         *
         * preferredOffset - the zone offset, null if no preference
         *
         * Returns:
         *
         * the zoned date-time, not null
         */

        ZonedDateTime zonedDateTime = ZonedDateTime.ofLocal(localDateTime,
                zoneId, zoneOffSet);
        System.out.println(zonedDateTime);

    }

}

Output

2018-02-11T09:52:38.932
Asia/Calcutta
Z
2018-02-11T09:52:38.932+05:30[Asia/Calcutta]

ZoneDateTimeDemo2.java

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class ZoneDateTimeDemo2
{

    public static void main(String[] args)
    {
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

        ZoneId zoneId = ZoneId.of("Z");
        System.out.println(zoneId);

        ZoneOffset zoneOffSet = ZoneOffset.UTC;
        System.out.println(zoneOffSet);

        /*
         * Parameters:
         *
         * localDateTime - the local date-time, not null
         *
         * offset - the zone offset, not null
         *
         * zone - the time-zone, not null
         *
         * Returns:
         *
         * the zoned date-time, not null
         *
         */


        ZonedDateTime zonedDateTime = ZonedDateTime.ofStrict(localDateTime,
                zoneOffSet, zoneId);
        System.out.println(zonedDateTime);
    }

}

Output

2018-02-11T09:52:49.746
Z
Z
2018-02-11T09:52:49.746Z

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

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

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