Friday 16 March 2018

How to use at methods of Year Class | Java 8 Date and Time


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

YearDemo1.java

import java.time.LocalDate;
import java.time.Year;
import java.time.YearMonth;

public class YearDemo1
{

    public static void main(String[] args)
    {

        Year year = Year.of(2017);
        /*
         * Parameters:
         *
         * dayOfYear - the day-of-year to use, from 1 to 365-366
         *
         * Returns:
         *
         * the local date formed from this year and the specified date
         * of year, not null
         */

        LocalDate localDate = year.atDay(300);
        System.out.println(localDate);

        /*
         * Parameters:
         *
         * month - the month-of-year to use, from 1 (January) to 12
         * (December)
         *
         * Returns:
         *
         * the year-month formed from this year and the specified
         * month, not null
         */

        YearMonth yearMonth = year.atMonth(2);
        System.out.println(yearMonth);

    }

}

Output

2017-10-27
2017-02

YearDemo2.java

import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;
import java.time.Year;

public class YearDemo2
{

    public static void main(String[] args)
    {
        Year year = Year.of(2017);
        System.out.println("year = "+year);

        MonthDay monthDay = MonthDay.of(Month.MARCH, 29);
        System.out.println("monthDay = "+monthDay);
       
        /*
         * Parameters:
         *
         * monthDay - the month-day to use, not null
         *
         * Returns:
         *
         * the local date formed from this year and the specified
         * month-day, not null
         */

        LocalDate localDate = year.atMonthDay(monthDay);
        System.out.println(localDate);
    }

}

Output

year = 2017
monthDay = --03-29
2017-03-29

YearDemo3.java

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

public class YearDemo3
{

    public static void main(String[] args)
    {
        Year year = Year.of(2017);

        /*
         * Parameters:
         *
         * month - the month-of-year to use, not null
         *
         * Returns:
         *
         * the year-month formed from this year and the specified
         * month, not null
         */


        YearMonth yearMonth = year.atMonth(Month.JANUARY);
        System.out.println(yearMonth);

    }

}

Output

2017-01

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

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

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