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 10, 2018 in Java by v.liyyah
• 1,300 points
68 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!

answered May 10, 2018 by geek.erkami
• 2,320 points
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 3 days ago by Sirajul
• 19,670 points

Related Questions In Java

0 votes
2 answers

What is the difference between Set and List in java?

LIST SET Duplicates YES NO Order Ordered Depends on Implementation Positional Access YES NO List is an ordered sequence ...READ MORE

answered Aug 14 in Java by Sirajul
• 19,670 points
87 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,870 points
56 views
0 votes
1 answer

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

Always TypeList is preferred over ArrayList. The ...READ MORE

answered Jul 9, 2018 in Java by Akrati
• 960 points
61 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
122 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,150 points
430 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 parth295
• 4,600 points
51 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,540 points
629 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,540 points
5,428 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,320 points
28 views
0 votes
2 answers

Listing all the subclasses of a specific class in Java

This is not possible to do using ...READ MORE

answered 3 days ago in Java by Sirajul
• 19,670 points
155 views