Friday, 12 January 2018

How to add month/Year and minus month/year from the current date | Java 8 LocalDate Class


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

LocalDateDemo1.java

import java.time.LocalDate;

public class LocalDateDemo1
{

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

        /*
         * Parameters:
         *
         * monthsToSubtract - the months to subtract, may be negative
         *
         * Returns:a LocalDate based on this date with the months
         * subtracted, not null
         */

        LocalDate dateAfterMonthChanged = date.minusMonths(2);
        System.out.println("dateAfterMonthChanged = " + dateAfterMonthChanged);

        /*
         * Parameters:
         *
         * yearsToSubtract - the years to subtract, may be negative
         *
         * Returns:a LocalDate based on this date with the years
         * subtracted, not null
         */

        LocalDate dateAfterYearChanged = date.minusYears(3);
        System.out.println("dateAfterYearChanged = " + dateAfterYearChanged);
    }

}

Output

date = 2018-01-04
dateAfterMonthChanged = 2017-11-04
dateAfterYearChanged = 2015-01-04

LocalDateDemo2.java

import java.time.LocalDate;

public class LocalDateDemo2
{

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

        /*
         * Parameters:
         *
         * monthsToAdd - the months to add, may be negative
         *
         * Returns:a LocalDate based on this date with the months
         * added, not null
         */

        LocalDate dateAfterMonthChanged = date.plusMonths(2);
        System.out.println("dateAfterMonthChanged = " + dateAfterMonthChanged);

        /*
         * Parameters:
         *
         * yearsToAdd - the years to add, may be negative
         *
         * Returns:a LocalDate based on this date with the years
         * added, not null
         */


        LocalDate dateAfterYearChanged = date.plusYears(5);
        System.out.println("dateAfterYearChanged = " + dateAfterYearChanged);
    }

}

Output

date = 2018-01-04
dateAfterMonthChanged = 2018-03-04
dateAfterYearChanged = 2023-01-04

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

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

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