Monday 26 March 2018

How to use isSupported method of YearMonth Class | Java 8 Date and Time


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

YearMonthDemo1.java

import java.time.YearMonth;
import java.time.temporal.ChronoField;

public class YearMonthDemo1
{

    public static void main(String[] args)
    {

        YearMonth yearMonth = YearMonth.now();

        /*
         * Parameters:
         *
         * field - the field to check, null returns false
         *
         * Returns:
         *
         * true if the field is supported on this year-month, false if
         * not
         */

        boolean isYearSupported = yearMonth.isSupported(ChronoField.YEAR);
        System.out.println("isYearSupported = " + isYearSupported);

        boolean isMonthYearSupported = yearMonth
                .isSupported(ChronoField.MONTH_OF_YEAR);
        System.out.println("isMonthYearSupported = " + isMonthYearSupported);

        boolean isDayOfMonthSupported = yearMonth
                .isSupported(ChronoField.DAY_OF_MONTH);
        System.out.println("isDayOfMonthSupported = " + isDayOfMonthSupported);

    }

}

Output

isYearSupported = true
isMonthYearSupported = true
isDayOfMonthSupported = false

YearMonthDemo2.java

import java.time.YearMonth;
import java.time.temporal.ChronoUnit;

public class YearMonthDemo2
{
    public static void main(String[] args)
    {
        YearMonth yearMonth = YearMonth.now();

        /*
         * Parameters:
         *
         * unit - the unit to check, null returns false
         *
         * Returns:
         *
         * true if the unit can be added/subtracted, false if not
         */

        boolean isYearSupported = yearMonth.isSupported(ChronoUnit.YEARS);
        System.out.println("isYearSupported = " + isYearSupported);

        boolean isDaysSupported = yearMonth.isSupported(ChronoUnit.DAYS);
        System.out.println("isDaysSupported = " + isDaysSupported);
    }

}

Output

isYearSupported = true
isDaysSupported = false

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

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

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