Thursday 15 March 2018

How to use parse and of methods of Year Class | Java 8 Date and Time


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

YearDemo1.java

import java.time.Year;

public class YearDemo1
{

    public static void main(String[] args)
    {
        /*
         * Parameters:
         *
         * text - the text to parse such as "2007", not null
         *
         * Returns:
         *
         * the parsed year, not null
         */

        Year year = Year.parse("2017");
        System.out.println(year);
    }

}

Output

2017

YearDemo2.java

import java.time.Year;
import java.time.format.DateTimeFormatter;

public class YearDemo2
{

    public static void main(String[] args)
    {
        DateTimeFormatter dateTimeFormatter =  DateTimeFormatter.ofPattern("yyyy");
        /*
         * Parameters:
         *
         * text - the text to parse, not null
         * formatter - the formatter to use, not null
         *
         * Returns:
         *
         * the parsed year, not null
         */

        Year year = Year.parse("2017",dateTimeFormatter);
        System.out.println(year);
    }

}

Output

2017

YearDemo3.java

import java.time.Year;

public class YearDemo3
{

    public static void main(String[] args)
    {
        /*
         * Parameters:
         *
         * isoYear - the ISO proleptic year to represent, from MIN_VALUE to MAX_VALUE
         *
         * Returns:
         *
         * the year, not null
         */

        Year year =  Year.of(2017);
        System.out.println(year);
    }

}

Output

2017

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

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

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