Monday, 12 February 2018

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


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

OffsetDateTimeDemo1.java

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class OffsetDateTimeDemo1
{

    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
         *
         * offset - the zone offset, not null
         *
         * Returns:
         *
         * the offset date-time, not null
         */

        OffsetDateTime offsetDateTime1 = OffsetDateTime.of(2017, 2, 28, 6, 50,
                40, 50000, ZoneOffset.UTC);
        System.out.println(offsetDateTime1);

    }

}

Output

2017-02-28T06:50:40.000050Z

OffsetDateTimeDemo2.java

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class OffsetDateTimeDemo2
{

    public static void main(String[] args)
    {

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

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


        OffsetDateTime offsetDateTime = OffsetDateTime.of(localDate, localTime,
                zoneOffset);
        System.out.println(offsetDateTime);

    }

}

Output

2017-02-03T12:30:30Z

OffsetDateTimeDemo3.java

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class OffsetDateTimeDemo3
{

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

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

        OffsetDateTime offsetDateTime = OffsetDateTime.of(localDateTime,
                zoneOffset);
        System.out.println(offsetDateTime);
    }

}

Output

2017-02-03T12:20:30Z

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/572947048c5892379e75d8e421fa231c20e2d3bc/BasicJava/OffsetDateTimeDemo_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