Thursday 29 March 2018

How to use isNegative and isZero methods of Period Class | Java 8 Date and Time


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

PeriodDemo.java

import java.time.Period;

public class PeriodDemo
{

    public static void main(String[] args)
    {

        Period period = Period.ofMonths(12);
        System.out.println("period = "+period);
       
        /*
         * Returns:
         *
         * true if any unit of this period is negative
         */

        boolean isNegative  = period.isNegative();
        System.out.println("isNegative = "+isNegative);
       
       
        /*
         * Checks if all three units of this period are zero.
         *
         * A zero period has the value zero for the years, months and
         * days units
         *
         */

        boolean isZero = period.isZero();
        System.out.println("isZero = "+isZero);

    }

}

Output

period = P12M
isNegative = false
isZero = false

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/755fea7aeca931764d09d2ae6d0900a44d7186bb/BasicJava_2018/PeriodDemo_isNegative_zero/?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