Javatpoint Logo
Javatpoint Logo

Getting a Range of Dates in Java

In Java, various methods for retrieving a range of dates exist. The most common alternatives are to use the built-in classes in the Java standard library, such as the LocalDate class, or third-party libraries, such as Joda-Time or the newer Java Time API introduced in Java 8.

In this section, we will discuss various techniques for obtaining a range of dates in Java and provide examples of how to use each one.

1. Using LocalDate Class

LocalDate is a Java built-in class that expresses a date without a time component. It is a component of the java. time package, which was introduced in Java 8.

Output:

Number of days between 2022-01-01 and 2022-12-31 is 31

Explanation

We may retrieve a range of dates by using LocalDate in two ways. The code produces two LocalDate objects representing the start and finish dates and uses the ChronoUnit.DAYS.between() function to compute the number of days between them.

DateArray.java

Output:

Number of days between 2023-05-01 and 2023-05-12 is 11
2023-05-01
2023-05-02
2023-05-03
2023-05-04
2023-05-05
2023-05-06
2023-05-07
2023-05-08
2023-05-09
2023-05-10
2023-05-11

2. Making Use of Calendar Classes

The Calendar class in Java is a heritage class that represents a date and time by including fields like year, month, day, hour, minute, and second.

To retrieve a range of dates, construct two Calendar objects representing the start and finish dates, and then use a loop to iterate over the dates in between.

DateRangeExample.java

Output:

Sat Jan 01 00:00:00 GMT 2022
Sun Jan 02 00:00:00 GMT 2022
Mon Jan 03 00:00:00 GMT 2022
Tue Jan 04 00:00:00 GMT 2022
Wed Jan 05 00:00:00 GMT 2022
Thu Jan 06 00:00:00 GMT 2022
Fri Jan 07 00:00:00 GMT 2022
Sat Jan 08 00:00:00 GMT 2022
Sun Jan 09 00:00:00 GMT 2022
Mon Jan 10 00:00:00 GMT 2022
Tue Jan 11 00:00:00 GMT 2022
Wed Jan 12 00:00:00 GMT 2022
Thu Jan 13 00:00:00 GMT 2022
Fri Jan 14 00:00:00 GMT 2022
Sat Jan 15 00:00:00 GMT 2022

Explanation

The start and end dates are represented as LocalDate objects, and the difference between them is calculated using the ChronoUnit class. This code generates two Calendar objects to represent the start and finish dates, then iterates over the dates in between using a while loop and the Calendar.add() function.

3. Using java.time.Period Class

The function makes use of the java.time class.Period class defines a time span between two dates in years, months, and days. Here's some code to demonstrate this method:

PeriodDate.java

Output:

Number of days between 2023-06-05 and 2023-06-22 is: 17
2023-06-05
2023-06-06
2023-06-07
2023-06-08
2023-06-09
2023-06-10
2023-06-11
2023-06-12
2023-06-13
2023-06-14
2023-06-15
2023-06-16
2023-06-17
2023-06-18
2023-06-19
2023-06-20
2023-06-21
2023-06-22

Explanation

Using the LocalDate.of() function, we construct two LocalDate objects representing the start and finish dates in this code. The period between the two dates is then calculated using the Period.between() function, which produces a duration object indicating the duration of time between two dates.

The Period object is then used to retrieve the number of years, months, and days.getYears(), getMonths(), and getDays() methods are available.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA