Tuesday, 6 February 2018

How to set year, month, day, hours, etc using with methods of OffsetDateTime Class


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

OffsetDateTimeDemo.java

import java.time.OffsetDateTime;

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);
       
        OffsetDateTime offsetDateTime2 = offsetDateTime1.withYear(2018);
        System.out.println("\nYear changed          = "+offsetDateTime2);

        OffsetDateTime offsetDateTime3 = offsetDateTime2.withMonth(10);
        System.out.println("Month changed         = "+offsetDateTime3);

        OffsetDateTime offsetDateTime4 = offsetDateTime3.withDayOfMonth(25);
        System.out.println("Day changed           = "+offsetDateTime4);

        OffsetDateTime offsetDateTime5 = offsetDateTime4.withHour(1);
        System.out.println("Hour changed          = "+offsetDateTime5);

        OffsetDateTime offsetDateTime6 = offsetDateTime5.withMinute(10);
        System.out.println("Min changed           = "+offsetDateTime6);

        OffsetDateTime offsetDateTime7 = offsetDateTime6.withSecond(20);
        System.out.println("Sec changed           = "+offsetDateTime7);

        OffsetDateTime offsetDateTime8 = offsetDateTime7.withNano(50);
        System.out.println("NanoSec changed       = "+offsetDateTime8);
       
    }

}

Output

offsetDateTime1       = 2017-02-03T10:15:30+01:00

Year changed          = 2018-02-03T10:15:30+01:00
Month changed         = 2018-10-03T10:15:30+01:00
Day changed           = 2018-10-25T10:15:30+01:00
Hour changed          = 2018-10-25T01:15:30+01:00
Min changed           = 2018-10-25T01:10:30+01:00
Sec changed           = 2018-10-25T01:10:20+01:00
NanoSec changed       = 2018-10-25T01:10:20.000000050+01:00

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

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

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