Wednesday, 28 March 2018

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


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

PeriodDemo1.java

import java.time.Period;

public class PeriodDemo1
{

    public static void main(String[] args)
    {

        Period periodOfDays = Period.ofDays(12);
        System.out.println("Before Plus, periodOfDays = " + periodOfDays);

        /*
         * Parameters:
         *
         * daysToAdd - the days to add, positive or negative
         *
         * Returns:
         *
         * a Period based on this period with the specified days
         * added, not null
         */

        periodOfDays = periodOfDays.plusDays(15);
        System.out.println("After Plus, periodOfDays = " + periodOfDays);
       
       
        Period periodOfMonths = Period.ofMonths(2);
        System.out.println("Before Plus, periodOfMonths = " + periodOfMonths);
       
        /*
         * Parameters:
         *
         * monthsToAdd - the months to add, positive or negative
         *
         * Returns:
         *
         * a Period based on this period with the specified months added, not null
         */

        periodOfMonths = periodOfMonths.plusMonths(5);
        System.out.println("After Plus, periodOfMonths = " + periodOfMonths);
    }

}

Output

Before Plus, periodOfDays = P12D
After Plus, periodOfDays = P27D
Before Plus, periodOfMonths = P2M
After Plus, periodOfMonths = P7M

PeriodDemo2.java

import java.time.Period;

public class PeriodDemo2
{
    public static void main(String[] args)
    {
        Period periodOfYears = Period.ofYears(2017);
        System.out.println("Before Plus, periodOfYears = " + periodOfYears);
       
        /*
         * Parameters:
         *
         * yearsToAdd - the years to add, positive or negative
         *
         * Returns:
         *
         * a Period based on this period with the specified years
         * added, not null
         */

        periodOfYears = periodOfYears.plusYears(13);
        System.out.println("After Plus, periodOfYears = " + periodOfYears);
       
       
        Period periodOfMonths = Period.ofMonths(2);
        System.out.println("Before Plus, periodOfMonths = " + periodOfMonths);
       
        /*
         * Parameters:
         *
         * amountToAdd - the amount to add, not null
         *
         * Returns:
         *
         * a Period based on this period with the requested period
         * added, not null
         */

        periodOfMonths = periodOfMonths.plus(Period.ofMonths(10));
        System.out.println("After Plus, periodOfMonths = " + periodOfMonths);
    }

}

Output

Before Plus, periodOfYears = P2017Y
After Plus, periodOfYears = P2030Y
Before Plus, periodOfMonths = P2M
After Plus, periodOfMonths = P12M

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/b8bbbd8e3fa258805c096cde7ca05dcc526111c2/BasicJava_2018/PeriodDemo_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