Friday 23 February 2018

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


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

ZoneDateTimeDemo1.java

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

public class ZoneDateTimeDemo1
{

    public static void main(String[] args)
    {

        /*
         * Parameters:
         *
         * year - the year to represent, from MIN_YEAR to MAX_YEAR
         *
         * month - the month-of-year to represent, from 1 (January) to
         * 12 (December)
         *
         * dayOfMonth - the day-of-month to represent, from 1 to 31
         *
         * hour - the hour-of-day to represent, from 0 to 23
         *
         * minute - the minute-of-hour to represent, from 0 to 59
         *
         * second - the second-of-minute to represent, from 0 to 59
         *
         * nanoOfSecond - the nano-of-second to represent, from 0 to
         * 999,999,999
         *
         * zone - the time-zone, not null
         *
         * Returns: the offset date-time, not null
         *
         */


        ZonedDateTime zonedDateTime = ZonedDateTime.of(2018, 2, 3, 6, 30, 40,
                50000, ZoneId.systemDefault());
        System.out.println(zonedDateTime);

    }

}

Output

2018-02-03T06:30:40.000050+05:30[Asia/Calcutta]

ZoneDateTimeDemo2.java

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class ZoneDateTimeDemo2
{

    public static void main(String[] args)
    {

        LocalDate localDate = LocalDate.parse("2017-02-03");
        LocalTime localTime = LocalTime.parse("12:30:30");

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

       
        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDate, localTime,ZoneId.systemDefault());
        System.out.println(zonedDateTime);

    }

}

Output

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

ZoneDateTimeDemo3.java

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

public class ZoneDateTimeDemo3
{

    public static void main(String[] args)
    {

        LocalDateTime localDateTime = LocalDateTime.now();

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

        ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime,
                ZoneId.systemDefault());
        System.out.println(zonedDateTime);

    }

}

Output

2018-02-09T09:59:31.014+05:30[Asia/Calcutta]

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

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

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