Tuesday, 6 February 2018

How to subtract year, month, day, hours, etc using minus methods of OffsetDateTime Class


Click here to watch on Youtube : 
https://www.youtube.com/watch?v=hElDnn3bCXU&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.minusYears(2);
        System.out.println("\nYear changed          = "+offsetDateTime2);

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

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

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

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

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

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

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

}

Output

2018-01-25T12:48:58.275+05:30

Year changed          = 2016-01-25T12:48:58.275+05:30
Month changed         = 2015-10-25T12:48:58.275+05:30
Day changed           = 2015-10-20T12:48:58.275+05:30
Hour changed          = 2015-10-20T11:48:58.275+05:30
Min changed           = 2015-10-20T11:38:58.275+05:30
Sec changed           = 2015-10-20T11:38:38.275+05:30
NanoSec changed       = 2015-10-20T11:38:38.274999950+05:30
Week changed          = 2015-09-29T11:38:38.274999950+05:30

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

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

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