Java 8 – How to get Number of Days in a Year from LocalDate ?

In this article, we will learn how to get number of Days in a Year from LocalDate and Calendar

1. Get Number of Days in a Year from LocalDate :

  • Sometimes, we need Number of Days in a Year from current LocalDate for some business requirement
  • To get number of days in a Year from LocalDate, we can use below method
    • lengthOfYear() – returns Number of Days in a Year from the invoking LocalDate
  • In the below illustration, we will
    • Get current LocalDate and find number of Days in a Year
    • Form LocalDate for Leap Year and find number of Days in that Year
  • Finally, print LocalDate and number of days to the console

GetLengthOfyearFromLocalDate.java

package in.bench.resources.java8.localdate.examples;

import java.time.LocalDate;
import java.time.Month;

public class GetLengthOfyearFromLocalDate {

	public static void main(String[] args) {

		// 1. get Current System Date
		LocalDate localDate = LocalDate.now();
		System.out.println("Current System Date is = " + localDate);


		// 1.1 find number of days in current Year
		int numberOfDays = localDate.lengthOfYear();
		System.out.println("Number of Days in Current Year-" 
				+ localDate.getYear() + " is = "
				+ numberOfDays);



		// 2. form LocalDate
		LocalDate localDate2 = LocalDate.of(2020, Month.MARCH, 20);
		System.out.println("\n\nLocalDate is = " + localDate2);


		// 2.1 find number of days in Leap Year
		int numberOfDays2 = localDate2.lengthOfYear();
		System.out.print("Number of Days in Leap Year-" 
				+ localDate.getYear() + " is = " 
				+ numberOfDays2);
	}
}

Output:

Current System Date is = 2022-07-31
Number of Days in Current Year-2022 is = 365


LocalDate is = 2020-03-20
Number of Days in Leap Year-2022 is = 366

2. Get Number of Days in a Year from Calendar/Date :

  • Below illustration finds number of Days from
    1. Current Year-2022
    2. Leap Year-2020

GetLengthOfYearFromCalendar.java

package in.bench.resources.java8.localdate.examples;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class GetLengthOfYearFromCalendar {

	public static void main(String[] args) {

		// 1. Instantiate GregorianCalendar for July-2022
		Calendar calendar = new GregorianCalendar(2022, Calendar.JULY, 31);
		System.out.println("Date is = " + calendar.getTime());


		// 1.1 Number of days in a Year-2022
		System.out.println("Number of Days in Current Year-"
				+ calendar.get(Calendar.YEAR) + " is = "
				+ calendar.getActualMaximum(Calendar.DAY_OF_YEAR));



		// 2. Instantiate GregorianCalendar for March-2020
		Calendar calendar2 = new GregorianCalendar(2020, Calendar.FEBRUARY, 1);
		System.out.println("\n\nDate is = " + calendar2.getTime());


		// 2.1 Number of days in in a Year-2020
		System.out.print("Number of Days in Leap Year-"
				+ calendar2.get(Calendar.YEAR) + " is = "
				+ calendar2.getActualMaximum(Calendar.DAY_OF_YEAR));
	}
}

Output:

Date is = Sun Jul 31 00:00:00 IST 2022
Number of Days in Current Year-2022 is = 365


Date is = Sat Feb 01 00:00:00 IST 2020
Number of Days in Leap Year-2020 is = 366

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to get remaining number of Days in a Year using LocalDate ?
Java 8 – How to get Number of Days in a Month from LocalDate ?