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
- Current Year-2022
- 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:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!