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,
- Day as 31st
- Month as December (11)
- 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:
- 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 !!