1

I'm working on Java Date(Java 8).
I have 2 dates, like: 2023-02-22 and 2023-02-28

My expect output contains:

2023-02-22, 2023-02-23, 2023-02-24, 2023-02-25, 2023-02-26, 2023-02-27, 2023-02-28
Date format: yyyy-MM-dd

My code:

public List<String> getDatesBetween(@NotEmpty String fromDate, @NotEmpty String toDate) throws Exception {
     List<String> dates = new ArrayList<>();
     // get dates between 2 dates
     
     return dates;
}

How can I get list date from this date? Thanks,

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Hungnn
  • 68
  • 1
  • 2
  • 19
  • Please include your attempt at solving this problem and what issues you faced. – Miss Skooter Feb 28 '23 at 07:49
  • 2
    It can be usefull - [https://www.baeldung.com/java-between-dates https://www.baeldung.com/java-string-to-date] – Ksu Feb 28 '23 at 08:02
  • Did you remember to search before asking? Asking because I’d be surprised if you didn’t find anything relevant. – Ole V.V. Feb 28 '23 at 08:19

2 Answers2

8

Java9+

You can use LocalDate::datesUntil which is introduced from Java9:

public Stream<LocalDate> datesUntil​(LocalDate endExclusive)
Returns a sequential ordered stream of dates. The returned stream starts from this date (inclusive) and goes to endExclusive (exclusive) by an incremental step of 1 day.
This method is equivalent to datesUntil(endExclusive, Period.ofDays(1)).

public List<String> getDatesBetween(@NotEmpty String fromDate, @NotEmpty String toDate){
    LocalDate startDate = LocalDate.parse(fromDate);
    LocalDate endDate = LocalDate.parse(toDate);
    return startDate.datesUntil(endDate.plusDays(1))
            .map(LocalDate::toString)
            .collect(Collectors.toList());
}

Java8

I notice that you edit your question to ask for a solution using Java8, in this case, you can use:

LocalDate startDate = LocalDate.parse(fromDate);
LocalDate endDate = LocalDate.parse(toDate);
return Stream.iterate(startDate, d -> d.plusDays(1))
        .limit(ChronoUnit.DAYS.between(startDate, endDate.plusDays(1)))
        .map(LocalDate::toString)
        .collect(Collectors.toList());
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
5

First of all, use LocalDate for handling dates. Whenever you deal with dates/times, you should strongly consider using java.time over Strings. You can convert Strings to LocalDates using LocalDate.parse:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate startDate = LocalDate.parse(fromDate, formatter);
LocalDate endDate = LocalDate.parse(toDate, formatter);

Then, you can just add one day over and over until you reach the same date:

for(LocalDate date = startDate; !endDate.isBefore(date); date = date.plusDays(1)){
    dates.add(date.format(formatter));
    //dates.add(date);//if you want to work with a List<LocalDate>
}

Summing up, you can use it in your method like this:

public List<String> getDatesBetween(@NotEmpty String fromDate, @NotEmpty String toDate) throws Exception {
    List<String> dates = new ArrayList<>();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDate startDate = LocalDate.parse(fromDate, formatter);
    LocalDate endDate =  LocalDate.parse(toDate, formatter);
    for(LocalDate date = startDate; !endDate.isBefore(date); date = date.plusDays(1)){   
      dates.add(date.format(formatter));
    }
    return dates;
}

As, shmosel mentioned in the comments, LocalDate uses the ISO_LOCAL_DATE by default so you don't have to provide a DateTimeFormatter in your case:

public List<String> getDatesBetween(@NotEmpty String fromDate, @NotEmpty String toDate) throws Exception {
    List<String> dates = new ArrayList<>();
    LocalDate startDate = LocalDate.parse(fromDate);
    LocalDate endDate =  LocalDate.parse(toDate);
    for(LocalDate date = startDate; !endDate.isBefore(date); date = date.plusDays(1)){
        dates.add(date.toString());
    }
    return dates;
}
dan1st
  • 12,568
  • 8
  • 34
  • 67