Monday 26 March 2018

How to get a year, month using get methods of YearMonth Class | Java 8 Date and Time


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

YearMonthDemo.java

import java.time.Month;
import java.time.YearMonth;

public class YearMonthDemo
{

    public static void main(String[] args)
    {

        YearMonth yearMonth = YearMonth.now();
        System.out.println("yearMonth = " + yearMonth);

        /*
         * Returns:
         *
         * the month-of-year, from 1 to 12
         */

        int monthValue = yearMonth.getMonthValue();
        System.out.println("monthValue = " + monthValue);

        /*
         * Returns:
         *
         * the year, from MIN_YEAR to MAX_YEAR
         */

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

        /*
         * Returns:
         *
         * the month-of-year, not null
         */

        Month month = yearMonth.getMonth();
        System.out.println("month = " + month);

    }

}

Output

yearMonth = 2018-03
monthValue = 3
year = 2018
month = MARCH

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

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

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