Thursday 15 March 2018

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


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

YearDemo1.java

import java.time.Year;

public class YearDemo1
{

    public static void main(String[] args)
    {
        Year year = Year.of(2017);
        System.out.println(year);
   
        /*
         * Returns:
         *
         * true if the year is leap, false otherwise
         */

        boolean isLeap = year.isLeap();
        System.out.println(year+ " is a leap year? = " + isLeap);
    }

}

Output

2017
2017 is a leap year? = false

YearDemo2.java

import java.time.Year;

public class YearDemo2
{

    public static void main(String[] args)
    {
        /*
         * Parameters:
         *
         * year - the year to check
         *
         * Returns:
         *
         * true if the year is leap, false otherwise
         */

        boolean isLeap = Year.isLeap(2012);
        System.out.println("2012 is a Leap year? = " + isLeap);
       
        isLeap = Year.isLeap(2014);
        System.out.println("2014 is a Leap year? = " + isLeap);
    }
   

}

Output

2012 is a Leap year? = true
2014 is a Leap year? = false

YearDemo3.java

import java.time.Year;

public class YearDemo3
{

    public static void main(String[] args)
    {
        Year year = Year.of(2017);
        System.out.println(year);
       
        /*
         * Returns:
         *
         * the length of this year in days, 365 or 366
         */

        int length = year.length();
       
        System.out.println("length = " + length);

    }

}

Output

2017
length = 365

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

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

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