Tuesday, 20 February 2018

How to subtract year, month etc from current date and time using minus methods of ZonedDateTime


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

ZoneDateTimeDemo.java

import java.time.ZonedDateTime;

public class ZoneDateTimeDemo
{

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

        ZonedDateTime zonedDateTime1 = ZonedDateTime.now();
        System.out.println("Current date and time = " + zonedDateTime1);

        ZonedDateTime zonedDateTime2 = zonedDateTime1.minusYears(2);
        System.out.println("\nYear changed          = " + zonedDateTime2);

        ZonedDateTime zonedDateTime3 = zonedDateTime2.minusMonths(3);
        System.out.println("Month changed         = " + zonedDateTime3);

        ZonedDateTime zonedDateTime4 = zonedDateTime3.minusDays(5);
        System.out.println("Day changed           = " + zonedDateTime4);

        ZonedDateTime zonedDateTime5 = zonedDateTime4.minusHours(1);
        System.out.println("Hour changed          = " + zonedDateTime5);

        ZonedDateTime zonedDateTime6 = zonedDateTime5.minusMinutes(10);
        System.out.println("Min changed           = " + zonedDateTime6);

        ZonedDateTime zonedDateTime7 = zonedDateTime6.minusSeconds(20);
        System.out.println("Sec changed           = " + zonedDateTime7);

        ZonedDateTime zonedDateTime8 = zonedDateTime7.minusNanos(50);
        System.out.println("NanoSec changed       = " + zonedDateTime8);

        ZonedDateTime zonedDateTime9 = zonedDateTime8.minusWeeks(3);
        System.out.println("Week changed          = " + zonedDateTime9);
    }

}

Output

Current date and time = 2018-02-05T10:11:57.559+05:30[Asia/Calcutta]

Year changed          = 2016-02-05T10:11:57.559+05:30[Asia/Calcutta]
Month changed         = 2015-11-05T10:11:57.559+05:30[Asia/Calcutta]
Day changed           = 2015-10-31T10:11:57.559+05:30[Asia/Calcutta]
Hour changed          = 2015-10-31T09:11:57.559+05:30[Asia/Calcutta]
Min changed           = 2015-10-31T09:01:57.559+05:30[Asia/Calcutta]
Sec changed           = 2015-10-31T09:01:37.559+05:30[Asia/Calcutta]
NanoSec changed       = 2015-10-31T09:01:37.558999950+05:30[Asia/Calcutta]
Week changed          = 2015-10-10T09:01:37.558999950+05:30[Asia/Calcutta]

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/6f1f0d2759f060503c7b9e4a4259ae3ddbfe139e/BasicJava/ZoneDateTimeDemo_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