Thursday, 29 March 2018

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


Click here to watch on Youtube:
https://www.youtube.com/watch?v=ETAd7aQ9m0Y&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);

        /*
         * Parameters:
         *
         * days - the days to represent, may be negative
         *
         * Returns:
         *
         * a Period based on this period with the requested days, not
         * null
         */


        Period period2 = period1.withDays(20);
        System.out.println("period2[withDays] = " + period2);

        /*
         * Parameters:
         *
         * years - the years to represent, may be negative
         *
         * Returns:
         *
         * a Period based on this period with the requested years, not
         * null
         */

        Period period3 = period2.withYears(2017);
        System.out.println("period3[withYears] = " + period3);
       
        /*
         * Parameters:
         *
         * months - the months to represent, may be negative
         *
         * Returns:
         *
         * a Period based on this period with the requested months,
         * not null
         */

        Period period4 = period3.withMonths(1);
        System.out.println("period4[withMonths]= " + period4);

    }

}

Output

period1 = P12M
period2[withDays] = P12M20D
period3[withYears] = P2017Y12M20D
period4[withMonths]= P2017Y1M20D

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

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

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