Fetch list of in-between dates using Java

0 votes

I have two dates(start and end date), but I want to fetch the list of all the dates lying between the start and the end date using Java. Can someone guide me, how to perform this?

May 11, 2018 in Java by v.liyyah
• 1,300 points
5,823 views

2 answers to this question.

0 votes

I tried generating the list of the in-between dates using Joda-Time. Check out the below code:

int myDates = Days.daysBetween(startDate, endDate).getDays();
List<LocalDate> dates = new ArrayList<LocalDate>(myDates);  // Set initial capacity to `days`.
for (int i=0; i < myDates; i++) {
    LocalDate dt = startDate.withFieldAdded(DurationFieldType.days(), i);
    dates.add(dt);
}

Hope this helps!

To know more about Java, It's recommended to join our Java Training in Chennai today.

answered May 11, 2018 by geek.erkami
• 2,680 points

edited Oct 7, 2021 by Sarfaraz
0 votes

java.time Package

The new java.time.package in Java 8 incorporates the features of the Joda-Time API.

You could probably try this code snippet:

String sd = "2019-08-01";
String ed = "2019-08-30";
LocalDate start = LocalDate.parse(sd);
LocalDate end = LocalDate.parse(ed);
List<LocalDate> totalDates = new ArrayList<>();
while (!start.isAfter(end)) {
               totalDates.add(start);
               start = start.plusDays(1);
}
answered Aug 21, 2019 by Sirajul
• 59,230 points

Related Questions In Java

0 votes
2 answers

I want list of date of every week based on pass dynamically "Mon ,Tue, Wed, Thu, Fri, Sat, Sun" between startDate and endDate using Java 8 LocaleDate

public List<LocalDate> getWeeklyDateByStringofDays(String daysOfWeek, LocalDate startDate, ...READ MORE

answered Sep 14, 2020 in Java by anonymous
• 190 points
1,465 views
0 votes
2 answers

What is the difference between Set and List in java?

List is an ordered sequence of elements. ...READ MORE

answered Apr 26, 2018 in Java by Akrati
• 3,190 points
63,360 views
0 votes
2 answers

How can I sort values of a Map in Java using its key

Assuming TreeMap is not good for you ...READ MORE

answered Oct 10, 2018 in Java by Sushmita
• 6,920 points
1,205 views
0 votes
2 answers

What is the difference between Type List and type ArrayList in Java

By List, you actually tell, that your object ...READ MORE

answered Aug 28, 2019 in Java by Sirajul
• 59,230 points
2,568 views
0 votes
3 answers

Increment the date in Java by 1-day

import java.time.LocalDate; public class DateIncrementer { static ...READ MORE

answered Aug 1, 2018 in Java by Akrati
• 3,190 points
4,536 views
0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by Parth
• 4,640 points
959 views
0 votes
1 answer

Convert Milliseconds to “X mins, x seconds” in Java?

I wrote a function which converts milliseconds ...READ MORE

answered May 30, 2018 in Java by Rishabh
• 3,620 points
3,108 views
0 votes
1 answer

How can I get the current date and time in UTC or GMT in Java?

This definitely returns UTC time: as String ...READ MORE

answered Jun 7, 2018 in Java by Rishabh
• 3,620 points
27,613 views
0 votes
1 answer

Working of post increment operator in Java

Well, I think the confusion is because ...READ MORE

answered May 23, 2018 in Java by geek.erkami
• 2,680 points
817 views
+2 votes
3 answers

Listing all the subclasses of a specific class in Java

Try with BurningWave core library. Here an ...READ MORE

answered Dec 29, 2019 in Java by anonymous
• 460 points

edited Jan 9, 2020 by anonymous 14,501 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP