-1

I want a list of dates between start date and end date, but with interval option,
if i select 30, get list of dates with 30 minutes interval in Java.

For example, 01/01/2018 10:00 TO 02/01/2018 10:00 should give

  • 01/01/2018 10:00
  • 01/01/2018 10:30
  • 01/01/2018 11:00
  • 01/01/2018 11:30
  • … etc.

Someone can help me ?

Thanks!

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    See [LocalDateTime](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html) which provides various methods such as `plusMinutes()`. – Andrew S Jul 10 '18 at 13:05
  • do you have some example my friend? – Lucas Bergamo Jul 10 '18 at 13:20
  • How many days are there in half an hour? :-) – Ole V.V. Jul 10 '18 at 13:25
  • 1
    Search. Then search a bit harder. It may not be the easiest questions to find, but this has been asked and answered more than once before. – Ole V.V. Jul 10 '18 at 13:26
  • thanks! i will search a little bit. – Lucas Bergamo Jul 10 '18 at 13:36
  • What does “dates with 30 minutes interval” mean? – Basil Bourque Jul 10 '18 at 15:14
  • 01/01/2018 10:00 TO 02/01/2018 10:00 -> 01/01/2018 10:00 / 01/01/2018 10:30 / 01/01/2018 11:00 / 01/01/2018 11:30 .........etc... sorry for my poor english – Lucas Bergamo Jul 10 '18 at 17:08
  • Thanks, @LucasBergamo, it’s great that you add an example, it makes it much clearer what you are after. When doing so, please edit the question using the edit link under it and add the supplementary information in the question itself. This time I did it for you. – Ole V.V. Jul 11 '18 at 13:47
  • I suggest that this question is a duplicate of [Getting a list of times between two times](https://stackoverflow.com/questions/15424032/getting-a-list-of-times-between-two-times). However that other question and its answers use the outdated and poorly designed `java.sql.Time` class, so you’re probably better off using [kumesana’s answer below](https://stackoverflow.com/a/51266670/5772882). – Ole V.V. Jul 11 '18 at 13:54

1 Answers1

1

Start with the start date. Then add the interval to it, until you reach the end date.

Example:

public static Iterator<LocalDateTime> datesBetween(LocalDateTime start, LocalDateTime end, int periodInMinutes) {
  return new DatesBetweenIterator(start, end, periodInMinutes);
}

private static class DatesBetweenIterator implements Iterator<LocalDateTime> {

  private LocalDateTime nextDate;
  private final LocalDateTime end;
  private final int periodInMinutes;

  private DatesBetweenIterator(LocalDateTime start, LocalDateTime end, int periodInMinutes) {
    this.nextDate = start;
    this.end = end;
    this.periodInMinutes = periodInMinutes;
  }

  @Override
  public boolean hasNext() {
    return nextDate.isBefore(end);
  }

  @Override
  public LocalDateTime next() {
    LocalDateTime toReturn = nextDate;
    nextDate = nextDate.plusMinutes(periodInMinutes);
    return toReturn;
  }
}
kumesana
  • 2,495
  • 1
  • 9
  • 10
  • i will try this and put in my business logic ! thanks kumesana! – Lucas Bergamo Jul 10 '18 at 13:36
  • just a question, Iterator datesBetween return a list ? – Lucas Bergamo Jul 10 '18 at 13:37
  • @LucasBergamo it returns an Iterator that you can iterate through to handle all the dates you requested. This avoids having to load them all in memory, and enables to stop in the middle if you find out you don't need to continue. If you need the full List loaded in memory, feel free to use the same idea but to add to a List instead of building an Iterator, or to just add to a List with forEachRemaining(). – kumesana Jul 10 '18 at 13:41