Wednesday 28 March 2018

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


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

YearMonthDemo.java

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

public class YearMonthDemo
{

    public static void main(String[] args)
    {

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

        YearMonth yearMonth2 = YearMonth.of(2000, Month.JANUARY);
        System.out.println("yearMonth2 = " + yearMonth2);

        /*
         * Parameters:
         *
         * endExclusive - the end date, exclusive, which is converted
         * to a YearMonth, not null
         *
         * unit - the unit to measure the amount in, not null
         *
         * Returns:
         *
         * the amount of time between this year-month and the end
         * year-month
         */

        long yearDifference = yearMonth1.until(yearMonth2, ChronoUnit.YEARS);
        System.out.println("yearDifference = " + yearDifference);

        long monthDifference = yearMonth1.until(yearMonth2, ChronoUnit.MONTHS);
        System.out.println("monthDifference = " + monthDifference);
    }

}

Output

yearMonth1 = 2014-11
yearMonth2 = 2000-01
yearDifference = -14
monthDifference = -178

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

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

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