Tuesday 27 March 2018

How to use with methods of YearMonth Class | Java 8 Date and Time


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

YearMonthDemo1.java

import java.time.YearMonth;

public class YearMonthDemo1
{

    public static void main(String[] args)
    {

        YearMonth yearMonth1 = YearMonth.of(2014, 11);
        System.out.println("yearMonth1 = " + yearMonth1);

        /*
         * Parameters:
         *
         * month - the month-of-year to set in the returned
         * year-month, from 1 (January) to 12 (December)
         *
         * Returns:
         *
         * a YearMonth based on this year-month with the requested
         * month, not null
         */

        YearMonth yearMonth2 = yearMonth1.withMonth(3);
        System.out.println("yearMonth2 = " + yearMonth2);

        /*
         * Parameters:
         *
         * year - the year to set in the returned year-month, from
         * MIN_YEAR to MAX_YEAR
         *
         * Returns:
         *
         * a YearMonth based on this year-month with the requested
         * year, not null
         */

        YearMonth yearMonth3 = yearMonth2.withYear(2017);
        System.out.println("yearMonth3 = " + yearMonth3);

    }

}


Output

yearMonth1 = 2014-11
yearMonth2 = 2014-03
yearMonth3 = 2017-03

YearMonthDemo2.java

import java.time.YearMonth;

public class YearMonthDemo2
{
    public static void main(String[] args)
    {
        YearMonth yearMonth1 = YearMonth.of(2014, 11);
        System.out.println("yearMonth1 = " + yearMonth1);
       
        YearMonth adjusterYearMonth= YearMonth.of(2013, 01);
        System.out.println("adjusterYearMonth = " + adjusterYearMonth);

        /*
         * Parameters:
         *
         * adjuster - the adjuster to use, not null
         *
         * Returns:
         *
         * a YearMonth based on this with the adjustment made, not
         * null
         */

        YearMonth yearMonth2 = yearMonth1.with(adjusterYearMonth);
        System.out.println("yearMonth2 = " + yearMonth2);
    }

}

Output

yearMonth1 = 2014-11
adjusterYearMonth = 2013-01
yearMonth2 = 2013-01

YearMonthDemo3.java

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

public class YearMonthDemo3
{
    public static void main(String[] args)
    {
        YearMonth yearMonth1 = YearMonth.of(2020, 11);
        System.out.println("yearMonth1 = " + yearMonth1);

        /*
         * Parameters:
         *
         * field - the field to set in the result, not null
         *
         * newValue - the new value of the field in the result
         *
         * Returns:
         *
         * a YearMonth based on this with the specified field set, not
         * null
         */

        YearMonth yearMonth2 = yearMonth1.with(ChronoField.YEAR,2013);
        System.out.println("yearMonth2 = " + yearMonth2);
    }

}

Output

yearMonth1 = 2020-11
yearMonth2 = 2013-11

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

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

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