Wednesday 24 January 2018

How to add year, month, day, hour etc to the date and time using plus methods of LocalDateTime Class


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

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

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

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

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

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

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

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

}

Output

Current date and Time = 2018-01-15T09:07:10.128

Year changed          = 2020-01-15T09:07:10.128
Month changed         = 2020-04-15T09:07:10.128
Day changed           = 2020-04-20T09:07:10.128
Hour changed          = 2020-04-20T10:07:10.128
Min changed           = 2020-04-20T10:17:10.128
Sec changed           = 2020-04-20T10:17:30.128
NanoSec changed       = 2020-04-20T10:17:30.128000050
Week changed          = 2020-05-11T10:17:30.128000050

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

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

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