Wednesday, 28 March 2018

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


Click here to watch on Youtube: 
https://www.youtube.com/watch?v=TVJZyevCtSY&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 Minus, periodOfDays = " + periodOfDays);

        /*
         * Parameters:
         *
         * daysToSubtract - the months to subtract, positive or
         * negative
         *
         * Returns:
         *
         * a Period based on this period with the specified days
         * subtracted, not null
         */

        periodOfDays = periodOfDays.minusDays(5);
        System.out.println("After Minus, periodOfDays = " + periodOfDays);
       
       
        Period periodOfMonths = Period.ofMonths(12);
        System.out.println("Before Minus, periodOfMonths = " + periodOfMonths);
       
        /*
         * Parameters:
         *
         * monthsToSubtract - the years to subtract, positive or
         * negative
         *
         * Returns:
         *
         * a Period based on this period with the specified months
         * subtracted, not null
         */

        periodOfMonths = periodOfMonths.minusMonths(5);
        System.out.println("After Minus, periodOfMonths = " + periodOfMonths);
    }

}

Output

Before Minus, periodOfDays = P12D
After Minus, periodOfDays = P7D
Before Minus, periodOfMonths = P12M
After Minus, 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 Minus, periodOfYears = " + periodOfYears);
       
        /*
         * Parameters:
         *
         * yearsToSubtract - the years to subtract, positive or
         * negative
         *
         * Returns:
         *
         * a Period based on this period with the specified years
         * subtracted, not null
         */

        periodOfYears = periodOfYears.minusYears(13);
        System.out.println("After Minus, periodOfYears = " + periodOfYears);
       
       
        Period periodOfMonths = Period.ofMonths(12);
        System.out.println("Before Minus, periodOfMonths = " + periodOfMonths);
       
        /*
         * Parameters:
         *
         * amountToSubtract - the amount to subtract, not null
         *
         * Returns:
         *
         * a Period based on this period with the requested period
         * subtracted, not null
         */

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

}

Output

Before Minus, periodOfYears = P2017Y
After Minus, periodOfYears = P2004Y
Before Minus, periodOfMonths = P12M
After Minus, periodOfMonths = P2M

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

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

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