Tuesday 20 February 2018

How to add year, month, hour etc to the current date and time using plus methods of ZonedDateTime


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

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

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

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

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

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

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

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

}

Output

Current date and time = 2018-02-06T09:23:30.033+05:30[Asia/Calcutta]

Year changed          = 2020-02-06T09:23:30.033+05:30[Asia/Calcutta]
Month changed         = 2020-05-06T09:23:30.033+05:30[Asia/Calcutta]
Day changed           = 2020-05-11T09:23:30.033+05:30[Asia/Calcutta]
Hour changed          = 2020-05-11T10:23:30.033+05:30[Asia/Calcutta]
Min changed           = 2020-05-11T10:33:30.033+05:30[Asia/Calcutta]
Sec changed           = 2020-05-11T10:33:50.033+05:30[Asia/Calcutta]
NanoSec changed       = 2020-05-11T10:33:50.033000050+05:30[Asia/Calcutta]
Week changed          = 2020-06-01T10:33:50.033000050+05:30[Asia/Calcutta]

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

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

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