Wednesday 28 March 2018

How to create a period object using of methods of Period Class | Java 8 Date and Time


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

PeriodDemo.java

import java.time.Period;

public class PeriodDemo
{

    public static void main(String[] args)
    {
        /*
         * Parameters:
         *
         * days - the number of days, positive or negative
         *
         * Returns:
         *
         * the period of days, not null
         */

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

        /*
         * Parameters:
         *
         * months - the number of months, positive or negative
         *
         * Returns:
         *
         * the period of months, not null
         */

        Period periodOfMonth = Period.ofMonths(12);
        System.out.println("periodOfMonth = " + periodOfMonth);

        /*
         * Parameters:
         *
         * years - the number of years, positive or negative
         *
         * Returns:
         *
         * the period of years, not null
         */

        Period periodOfYears = Period.ofYears(2017);
        System.out.println("periodOfYears = " + periodOfYears);

        /*
         * Parameters:
         *
         * weeks - the number of weeks, positive or negative
         *
         * Returns:
         *
         * the period, with the input weeks converted to days, not
         * null
         */

        Period periodWeekToDays = Period.ofWeeks(2);
        System.out.println("periodWeekToDays = " + periodWeekToDays);

    }

}

Output

periodOfDays = P12D
periodOfMonth = P12M
periodOfYears = P2017Y
periodWeekToDays = P14D

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

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

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