Monday, 12 February 2018

How to convert OffsetDateTime to LocalDate, ZonedDateTime etc | OffsetDateTime Class


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

OffsetDateTimeDemo.java

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;

public class OffsetDateTimeDemo
{

    public static void main(String[] args)
    {

        OffsetDateTime offsetDateTime = OffsetDateTime.now();
        System.out.println("offsetDateTime = " + offsetDateTime);

        Instant instant = offsetDateTime.toInstant();
        System.out.println("instant = " + instant);

        LocalDate localDate = offsetDateTime.toLocalDate();
        System.out.println("localDate = " + localDate);

        LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
        System.out.println("localDateTime = " + localDateTime);

        LocalTime localTime = offsetDateTime.toLocalTime();
        System.out.println("localTime = " + localTime);

        OffsetTime offsetTime = offsetDateTime.toOffsetTime();
        System.out.println("offsetTime = " + offsetTime);

        ZonedDateTime zonedDateTime = offsetDateTime.toZonedDateTime();
        System.out.println("zonedDateTime = " + zonedDateTime);
    }

}

Output

offsetDateTime = 2018-01-30T09:24:53.355+05:30
instant = 2018-01-30T03:54:53.355Z
localDate = 2018-01-30
localDateTime = 2018-01-30T09:24:53.355
localTime = 09:24:53.355
offsetTime = 09:24:53.355+05:30
zonedDateTime = 2018-01-30T09:24:53.355+05:30

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

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

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