Wednesday 21 March 2018

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


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

YearMonthDemo1.java

import java.time.Clock;
import java.time.YearMonth;

public class YearMonthDemo1
{

    public static void main(String[] args)
    {

        Clock clock = Clock.systemDefaultZone();
        System.out.println("clock = "+clock);
        /*
         * Parameters:
         *
         * clock - the clock to use, not null
         *
         * Returns:
         *
         * the current year-month, not null
         */

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

    }

}

Output

clock = SystemClock[Asia/Calcutta]
yearMonth = 2018-02

YearMonthDemo2.java

import java.time.YearMonth;
import java.time.ZoneId;

public class YearMonthDemo2
{

    public static void main(String[] args)
    {

        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println("zoneId = "+zoneId);
        /*
         * Parameters:
         *
         * zone - the zone ID to use, not null
         *
         * Returns:
         *
         * the current year-month using the system clock, not null
         */

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

}

Output

zoneId = Asia/Calcutta
yearMonth = 2018-02

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

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

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