Thursday 15 March 2018

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


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

YearDemo1.java

import java.time.Clock;
import java.time.Year;

public class YearDemo1
{

    public static void main(String[] args)
    {
        Clock clock = Clock.systemDefaultZone();
        System.out.println(clock);
        /*
         * Parameters:
         *
         * clock - the clock to use, not null
         *
         * Returns:the current year, not null
         */

        Year year = Year.now(clock);
        System.out.println(year);
    }

}

Output

SystemClock[Asia/Calcutta]
2018

YearDemo2.java

import java.time.Year;
import java.time.ZoneId;

public class YearDemo2
{

    public static void main(String[] args)
    {
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println(zoneId);
        /*
         * Parameters:
         *
         * zone - the zone ID to use, not null
         *
         * Returns:
         *
         * the current year using the system clock, not null
         */

        Year year = Year.now(zoneId);
        System.out.println(year);
    }

}

Output

Asia/Calcutta
2018

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/8e3f14caa10b52912934997a22df069c728f47b4/BasicJava_2018/YearDemo_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