Wednesday 24 January 2018

How to minus year, month, day, etc from the date and time using minus methods of LocalDateTime


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

LocalDateTimeDemo.java

import java.time.LocalDateTime;

public class LocalDateTimeDemo
{

    public static void main(String[] args)
    {
        LocalDateTime localDateTime1 = LocalDateTime.now();
        System.out.println("Current date and Time = " + localDateTime1);

        LocalDateTime localDateTime2 = localDateTime1.minusYears(2);
        System.out.println("\nYear changed          = " + localDateTime2);

        LocalDateTime localDateTime3 = localDateTime2.minusMonths(3);
        System.out.println("Month changed         = " + localDateTime3);

        LocalDateTime localDateTime4 = localDateTime3.minusDays(5);
        System.out.println("Day changed           = " + localDateTime4);

        LocalDateTime localDateTime5 = localDateTime4.minusHours(1);
        System.out.println("Hour changed          = " + localDateTime5);

        LocalDateTime localDateTime6 = localDateTime5.minusMinutes(10);
        System.out.println("Min changed           = " + localDateTime6);

        LocalDateTime localDateTime7 = localDateTime6.minusSeconds(20);
        System.out.println("Sec changed           = " + localDateTime7);

        LocalDateTime localDateTime8 = localDateTime7.minusNanos(50);
        System.out.println("NanoSec changed       = " + localDateTime8);

        LocalDateTime localDateTime9 = localDateTime8.minusWeeks(3);
        System.out.println("Week changed          = " + localDateTime9);

    }

}

Output

Current date and Time = 2018-01-13T08:30:09.954

Year changed          = 2016-01-13T08:30:09.954
Month changed         = 2015-10-13T08:30:09.954
Day changed           = 2015-10-08T08:30:09.954
Hour changed          = 2015-10-08T07:30:09.954
Min changed           = 2015-10-08T07:20:09.954
Sec changed           = 2015-10-08T07:19:49.954
NanoSec changed       = 2015-10-08T07:19:49.953999950
Week changed          = 2015-09-17T07:19:49.953999950

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/2732bd5046563d647669928b143687e5dbf4d99f/BasicJava/LocalDateTimeDemo_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