Wednesday 21 March 2018

How to add year and month using plus methods of YearMonth Class | Java 8 Date and Time


Click here to watch on Youtube: 
https://www.youtube.com/watch?v=d1x2uqWgPPk&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:
         *
         * yearsToAdd - the years to add, may be negative
         *
         * Returns:
         *
         * a YearMonth based on this year-month with the years
         * added, not null
         */

        YearMonth yearMonth2 = yearMonth1.plusYears(5);
        System.out.println("yearMonth2 = " + yearMonth2);
       
        /*
         * Parameters:
         *
         * monthsToAdd - the months to add, may be negative
         *
         * Returns:
         *
         * a YearMonth based on this year-month with the
         * months added, not null
         */

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

}

Output

yearMonth1 = 2018-03
yearMonth2 = 2023-03
yearMonth3 = 2023-05

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

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

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

    }

}

Output

yearMonth1 = 2018-03
yearMonth2 = 2048-03
yearMonth3 = 2048-04

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:
         *
         * amountToAdd - the amount to add, not null
         *
         * Returns:
         *
         * a YearMonth based on this year-month with the addition
         * made, not null
         */

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

}

Output

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

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

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

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

    Post a Comment