Wednesday 24 January 2018

How to convert LocalDateTime to ZonedDateTime using atZone method of LocalDateTime Class


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

LocalDateTimeDemo.java

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

public class LocalDateTimeDemo
{

    public static void main(String[] args)
    {
        LocalDateTime localDateTime = LocalDateTime
                .parse("2017-05-03T12:30:30");

        System.out.println(localDateTime);

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

        /*
         * Parameters:
         *
         * zone - the time-zone to use, not null
         *
         * Returns:
         *
         * the zoned date-time formed from this date-time, not null
         */

        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);

        System.out.println(zonedDateTime);

    }

}

Output

2017-05-03T12:30:30
zoneId = Asia/Calcutta
2017-05-03T12:30:30+05:30[Asia/Calcutta]

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/2732bd5046563d647669928b143687e5dbf4d99f/BasicJava/LocalDateTimeDemo_atZone/?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