Tuesday 5 June 2018

How to use get methods of Month Enum | Java 8 Date and Time


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

MonthDemo1.java

import java.time.Month;
import java.time.temporal.ChronoField;

public class MonthDemo1
{
    public static void main(String[] args)
    {
        Month month = Month.NOVEMBER;
        System.out.println("month = " + month);

        /*
         * Gets the value of the specified field from this
         * month-of-year as an int.
         *
         * Parameters:
         *
         * field - the field to get, not null
         *
         * Returns:
         *
         * the value for the field, within the valid range of values
         */

        int monthOfYear = month.get(ChronoField.MONTH_OF_YEAR);
        System.out.println("monthOfYear = " + monthOfYear);
    }

}

Output

month = NOVEMBER
monthOfYear = 11

MonthDemo2.java

import java.time.Month;
import java.time.temporal.ChronoField;

public class MonthDemo2
{
    public static void main(String[] args)
    {
        Month month = Month.MARCH;
        System.out.println("month = " + month);

        /*
         * Gets the value of the specified field from this
         * month-of-year as a long.
         *
         * Parameters:
         *
         * field - the field to get, not null
         *
         * Returns:
         *
         * the value for the field, within the valid range of values
         */

        long monthOfYear = month.getLong(ChronoField.MONTH_OF_YEAR);
        System.out.println("monthOfYear = " + monthOfYear);
    }

}

Output

month = MARCH
monthOfYear = 3

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/8781cb2c398cca3734b090d3a80e4b0023df3f71/BasicJava_2018/MonthDemo_get_methods/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment