Monday 26 March 2018

How to subtract year and month using minus methods of YearMonth Class | Java 8 Date and Time


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

YearMonthDemo1.java

import java.time.YearMonth;

public class YearMonthDemo1
{

    public static void main(String[] args)
    {

        YearMonth yearMonth1 = YearMonth.now();

        System.out.println("yearMonth1 = " + yearMonth1);

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

        YearMonth yearMonth2 = yearMonth1.minusYears(5);
        System.out.println("yearMonth2 = " + yearMonth2);

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

        YearMonth yearMonth3 = yearMonth2.minusMonths(2);
        System.out.println("yearMonth3 = " + yearMonth3);
    }

}

Output

yearMonth1 = 2018-03
yearMonth2 = 2013-03
yearMonth3 = 2013-01

YearMonthDemo2.java

import java.time.YearMonth;
import java.time.temporal.ChronoUnit;

public class YearMonthDemo2
{

    public static void main(String[] args)
    {

        YearMonth yearMonth1 = YearMonth.now();

        System.out.println("yearMonth1 = " + yearMonth1);

        /*
         * 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 YearMonth based on this year-month with the specified
         * amount subtracted, not null
         */

        YearMonth yearMonth2 = yearMonth1.minus(30, ChronoUnit.YEARS);
        System.out.println("yearMonth2 = " + yearMonth2);

        YearMonth yearMonth3 = yearMonth2.minus(1, ChronoUnit.MONTHS);
        System.out.println("yearMonth3 = " + yearMonth3);

    }

}

Output

yearMonth1 = 2018-03
yearMonth2 = 1988-03
yearMonth3 = 1988-02

YearMonthDemo3.java

import java.time.Period;
import java.time.YearMonth;

public class YearMonthDemo3
{

    public static void main(String[] args)
    {

        YearMonth yearMonth1 = YearMonth.now();
        System.out.println("yearMonth1 = " + yearMonth1);

        Period period = Period.ofYears(9);
        System.out.println("period = " + period);

        /*
         * Parameters:
         *
         * amountToSubtract - the amount to subtract, not null
         *
         * Returns:
         *
         * a YearMonth based on this year-month with the subtraction
         * made, not null
         */

        YearMonth yearMonth2 = yearMonth1.minus(period);
        System.out.println("yearMonth2 = " + yearMonth2);
    }

}

Output

yearMonth1 = 2018-03
period = P9Y
yearMonth2 = 2009-03

Click the below link to download the code:
https://sites.google.com/site/ramj2eev2/java_basics/YearMonthDemo_minus.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava_2018/YearMonthDemo_minus

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/b8bbbd8e3fa258805c096cde7ca05dcc526111c2/BasicJava_2018/YearMonthDemo_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
  • Cooking Tutorial
  • No comments:

    Post a Comment