Tuesday 6 February 2018

How to add year, month, day, hours, etc using plus methods of OffsetDateTime Class


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

OffsetDateTimeDemo.java

import java.time.OffsetDateTime;

public class OffsetDateTimeDemo
{

    public static void main(String[] args)
    {
        /*
         * Returns:the current date-time using the system clock, not
         * null.
         */

        OffsetDateTime offsetDateTime1 = OffsetDateTime.now();
        System.out.println(offsetDateTime1);
       
        OffsetDateTime offsetDateTime2 = offsetDateTime1.plusYears(2);
        System.out.println("\nYear changed          = "+offsetDateTime2);

        OffsetDateTime offsetDateTime3 = offsetDateTime2.plusMonths(3);
        System.out.println("Month changed         = "+offsetDateTime3);

        OffsetDateTime offsetDateTime4 = offsetDateTime3.plusDays(5);
        System.out.println("Day changed           = "+offsetDateTime4);

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

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

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

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

        OffsetDateTime offsetDateTime9 = offsetDateTime8.plusWeeks(3);
        System.out.println("Week changed          = "+offsetDateTime9);
       
    }

}

Output

2018-01-25T13:01:11.660+05:30

Year changed          = 2020-01-25T13:01:11.660+05:30
Month changed         = 2020-04-25T13:01:11.660+05:30
Day changed           = 2020-04-30T13:01:11.660+05:30
Hour changed          = 2020-04-30T14:01:11.660+05:30
Min changed           = 2020-04-30T14:11:11.660+05:30
Sec changed           = 2020-04-30T14:11:31.660+05:30
NanoSec changed       = 2020-04-30T14:11:31.660000050+05:30
Week changed          = 2020-05-21T14:11:31.660000050+05:30

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

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

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