Thursday 29 March 2018

How to use negated method of Period Class | Java 8 Date and Time


Click here to watch on Youtube: 
https://www.youtube.com/watch?v=PDklNB-6mG0&list=UUhwKlOVR041tngjerWxVccw

PeriodDemo.java

import java.time.Period;

public class PeriodDemo
{

    public static void main(String[] args)
    {

        Period period1 = Period.ofMonths(12);
        System.out.println("period1 = " + period1);
        /*
         * Returns a new instance with each amount in this period
         * negated.
         *
         * This returns a period with each of the years, months and
         * days units individually negated. For example, a period of
         * "2 years, -3 months and 4 days" will be negated to
         * "-2 years, 3 months and -4 days".
         *
         * Returns:
         *
         * a Period based on this period with the amounts negated, not
         * null
         */

        Period period2 = period1.negated();
        System.out.println("period2 = " + period2);

    }

}

Output

period1 = P12M
period2 = P-12M

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

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

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