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

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

1. Get remaining number of Days in a Year using LocalDate :

  • To get number of days remaining in a Year using LocalDate, we can use ChronoUnit.DAYS.between() method
  • ChronoUnit.DAYS.between accepts 2 input-arguments as LocalDate and returns difference of two LocalDate as Days in long format
    • Pass 1st argument as the current System Date
    • Pass 2nd argument as the last day of the current Year
  • Finally print current System Date, Last date of the current Year and number of days remaining to the console

GetRemainingDaysInYearUsingLocalDate.java

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

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class GetRemainingDaysInYearUsingLocalDate {

	public static void main(String[] args) {

		// 1. get current system Date
		LocalDate localDate1 = LocalDate.now();
		System.out.println("Current System Date is :- \n" + localDate1);


		// 2. get last day of the year
		LocalDate localDate2 = LocalDate.of(localDate1.getYear(), 12, 31);
		System.out.println("\nLast Day of the Year is :- \n" + localDate2);


		// 3. days remaining in current year
		long daysRemaining = ChronoUnit.DAYS.between(localDate1, localDate2);
		System.out.print("\nNumber of Days remaining in Year-" 
				+ localDate1.getYear() 
				+ " is :- \n" + daysRemaining);
	}
}

Output:

Current System Date is :- 
2022-07-31

Last Day of the Year is :- 
2022-12-31

Number of Days remaining in Year-2022 is :- 
153

2. Get remaining number of Days in a Year using Calendar/Date :

  • Get current Instance from Calendar using Calender.getInstance() method
    • calendar.get(Calendar.DAY_OF_YEAR) method returns number of Days passed from start of Year
  • Instantiate GregorianCalendar passing last day of the current Year with,
    1. Day as 31st
    2. Month as December (11)
    3. Year as current year (2022)
  • Next, get numbers of days in current Year (2022)
  • Finally subtract numbers of days passed from number of days in a Year which will give days remaining in current Year

GetRemainingDaysInYearUsingCalendarDate.java

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

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

public class GetRemainingDaysInYearUsingCalendarDate {

	public static void main(String[] args) {

		// 1. get current Date
		Calendar calendar1 = Calendar.getInstance();
		System.out.println("Current date is :- \n" 
				+ calendar1.getTime());


		// 1.1 get current year
		int year = calendar1.get(Calendar.YEAR);


		// 1.2 get number of days
		int dayOfYear1 = calendar1.get(Calendar.DAY_OF_YEAR);
		System.out.println("\nNumber of days from start of Year-" 
				+ year 
				+ " till today :- \n" 
				+ dayOfYear1);


		// 2. Instantiate GregorianCalendar
		Calendar calendar2 = new GregorianCalendar(year, 11, 31);
		System.out.println("\nLast day of the current Year-" 
				+ year 
				+ " is :- \n"
				+ calendar2.getTime());


		// 2.1 get number of days in a Year
		int dayOfYear2 = calendar2.get(Calendar.DAY_OF_YEAR);
		System.out.println("\nNumber of days in current Year-"
				+ year
				+ " is :- \n" 
				+ dayOfYear2);



		// 3. Number of remaining days
		int daysRemaining = dayOfYear2 - dayOfYear1;
		System.out.print("\nNumber of Days remaining in Year-"
				+ year
				+ " is :- \n" 
				+ daysRemaining);
	}
}

Output:

Current date is :- 
Sun Jul 31 16:28:10 IST 2022

Number of days from start of Year-2022 till today :- 
212

Last day of the current Year-2022 is :- 
Sat Dec 31 00:00:00 IST 2022

Number of days in current Year-2022 is :- 
365

Number of Days remaining in Year-2022 is :- 
153

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to find difference between two LocalDate instances using Period ?
Java 8 – How to get Number of Days in a Year from LocalDate ?