Wednesday, 28 March 2018

Java 8 Period Class Introduction | Java 8 Date and Time | Java Date and Time


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

Click the below Image to Enlarge:
Java 8 Period Class Introduction | Java 8 Date and Time | Java Date and Time
PeriodDemo1.java

import java.time.Period;

public class PeriodDemo1
{

    public static void main(String[] args)
    {
        /*
         * Parameters:
         *
         * years - the amount of years, may be negative
         *
         * months - the amount of months, may be negative
         *
         * days - the amount of days, may be negative
         *
         * Returns:
         *
         * the period of years, months and days, not null
         */

        Period period = Period.of(2017, 02, 16);
        System.out.println("period = " + period);

        /*
         * Returns: the amount of years of this period, may be
         * negative
         */

        System.out.println("Years = " + period.getYears());

        /*
         * Returns: the amount of months of this period, may be
         * negative
         */

        System.out.println("Months = " + period.getMonths());

        /*
         * Returns: the amount of days of this period, may be negative
         */

        System.out.println("Days = " + period.getDays());
    }

}

Output

period = P2017Y2M16D
Years = 2017
Months = 2
Days = 16

PeriodDemo2.java

import java.time.Period;

public class PeriodDemo2
{
    public static void main(String[] args)
    {
        /*
         * A constant for a period of zero.
         */

        Period period = Period.ZERO;

        System.out.println("period = "+period);
    }

}

Output

period = P0D

Refer: 
https://docs.oracle.com/javase/8/docs/api/index.html?java/time/Period.html

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

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

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