Wednesday 24 January 2018

How to use plus, minus methods which accept long and temporal unit | LocalDateTime Class


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

LocalDateTimeDemo.java

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class LocalDateTimeDemo
{

    public static void main(String[] args)
    {
        LocalDateTime currentDatetime = LocalDateTime.now();
        System.out.println("currentDatetime= " + currentDatetime);

        /*
         * Parameters:
         *
         * amountToAdd - the amount of the unit to add to the result,
         * may be negative
         *
         * unit - the unit of the amount to add, not null
         *
         * Returns:
         *
         * a LocalDateTime based on this date-time with the specified
         * amount added, not null
         */

        LocalDateTime localDateTime2 = currentDatetime.plus(5,
                ChronoUnit.MONTHS);
        System.out.println("Month Added    = " + localDateTime2);

        /*
         * Parameters:
         *
         * amountToSubtract - the amount of the unit to subtract from
         * the result, may be negative
         *
         * unit - the unit of the amount to subtract, not null
         *
         * Returns:
         *
         * a LocalDateTime based on this date-time with the specified
         * amount subtracted, not null
         */


        LocalDateTime localDateTime3 = currentDatetime.minus(5,
                ChronoUnit.YEARS);
        System.out.println("Years reduced  = " + localDateTime3);
    }

}

Output

currentDatetime= 2018-01-16T09:44:12.673
Month Added    = 2018-06-16T09:44:12.673
Years reduced  = 2013-01-16T09:44:12.673

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

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

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