Friday 9 February 2018

How to set dayofyear, zone offset using with methods of OffsetDateTime Class | Java 8 Date and Time


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

OffsetDateTimeDemo.java

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

public class OffsetDateTimeDemo
{

    public static void main(String[] args)
    {
        OffsetDateTime offsetDateTime1 = OffsetDateTime
                .parse("2017-02-03T10:15:30+01:00");
        System.out.println("offsetDateTime1       = " + offsetDateTime1);
        /*
         * Parameters:
         *
         * dayOfYear - the day-of-year to set in the result, from 1 to
         * 365-366
         *
         * Returns:an OffsetDateTime based on this date with the
         * requested day, not null
         */

        OffsetDateTime offsetDateTime2 = offsetDateTime1.withDayOfYear(150);
        System.out.println("DayOfYear changed     = " + offsetDateTime2);

        /*
         * Parameters:
         *
         * offset - the zone offset to change to, not null
         *
         * Returns:an OffsetDateTime based on this date-time with the
         * requested offset, not null
         */

        OffsetDateTime offsetDateTime3 = offsetDateTime2
                .withOffsetSameInstant(ZoneOffset.UTC);
        System.out.println("Offset changed        = " + offsetDateTime3);
    }

}

Output

offsetDateTime1       = 2017-02-03T10:15:30+01:00
DayOfYear changed     = 2017-05-30T10:15:30+01:00
Offset changed        = 2017-05-30T09:15:30Z

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

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

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