Wednesday, 28 March 2018

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


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

PeriodDemo.java

import java.time.Period;

public class PeriodDemo
{

    public static void main(String[] args)
    {
        /*
         * Parameters:
         *
         * text - the text to parse, not null
         *
         * Examples of valid input:
         *
         * "P2Y" -- Period.ofYears(2)
         * "P3M" -- Period.ofMonths(3)
         * "P4W" -- Period.ofWeeks(4)
         * "P5D" -- Period.ofDays(5)
         * "P2Y2M3D" -- Period.of(2, 2, 3)
         * "P2Y2M3W4D" -- Period.of(2, 2, 25)
         * "P-2Y2M" -- Period.of(-2, 2, 0)
         * "-P2Y2M" -- Period.of(-2, -2, 0)
         *
         * Returns:
         *
         * the parsed period, not null
         *
         */

        Period period = Period.parse("P5D");

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

    }

}

Output

period = P5D

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

Github Link:
https://bitbucket.org/ramram43210/java/src/b8bbbd8e3fa258805c096cde7ca05dcc526111c2/BasicJava_2018/PeriodDemo_parse/?at=master

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