-2

With the help of an answer from how to get a list of dates between two dates in java,

How can I get a list of DateTime-Local between two DateTime-Local?

Below are the codes

LocalDateTime fromDate = LocalDateTime.parse("2007-12-03T10:15");
LocalDateTime toDate = LocalDateTime.parse("2107-12-03T10:15");

List<LocalDateTime> dates = new ArrayList<LocalDateTime>();

LocalDateTime current = fromDate;

while (current.isBefore(toDate)) {
    dates.add(current);
    current = current.plusDays(1);
}

System.out.println(dates); 

It prints out [2007-12-03T10:15, 2007-12-04T10:15, 2007-12-05T10:15, 2007-12-06T10:15, 2007-12-07T10:15, 2007-12-08T10:15...

The date has increased by a day, however, the time doesn't.

How can I increase the time too?

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Emma E
  • 49
  • 10
  • 1
    I don't understand the expected output. Let's guess we have the new date at 12am : you want to get 10+2 (from original date), or 10+12 ? Can you give some example? – Leviand Aug 27 '18 at 12:17
  • @Leviand Is there a difference between this two ? Is yes, 10+12 – Emma E Aug 27 '18 at 12:22
  • I still can't get the expected output, what should be in your above example? – Leviand Aug 27 '18 at 12:24
  • 5
    It's unclear what you are asking. You wrote `current = current.plusDays(1);` so the date is incremented by a day. What else did you expect? If you want a 1-hour increment, use `current = current.plusHours(1);`. If you want a 1-minute increment, use `current = current.plusMinutes(1);`. Etc. – assylias Aug 27 '18 at 13:43
  • @assylias Sorry for the unclear question. It is my mistake for writing `plusDays` Your answer is what I am looking for, where I want to increment by hours. Thank you! – Emma E Aug 27 '18 at 15:19

1 Answers1

0

I guess a Spliterator can be used to go through the difference between dates. For example, for hourly increase, HourlySpliterator can be implemented as follows:

public class HourlySpliterator implements Spliterator<LocalDateTime>{

private final LocalDateTime endDate;
private LocalDateTime date;

public static Stream<LocalDateTime> of (LocalDateTime startDate,  LocalDateTime endDate) {
    return StreamSupport.stream(new HourlySpliterator(startDate, endDate), false);
}

private HourlySpliterator(LocalDateTime startDate, LocalDateTime endDate) {
    this.date = startDate;
    this.endDate = endDate;
}

@Override
public boolean tryAdvance(Consumer<? super LocalDateTime> action) {
    Objects.requireNonNull(action);
    action.accept(date);
    if (date.isBefore(endDate)) {
        date = date.plusHours(1);
        return true;
    }
    return false;
}

@Override
public Spliterator<LocalDateTime> trySplit() {
    return null;
}

@Override
public long estimateSize() {
    return Long.MAX_VALUE;
}

@Override
public int characteristics() {
    return DISTINCT | IMMUTABLE | NONNULL | ORDERED | SORTED ;
}

@Override
public Comparator<? super LocalDateTime> getComparator() {
   return Comparator.naturalOrder();
}

}

It can be tested as:

@Test
public void test() throws Exception {

        List<LocalDateTime> dates = new ArrayList<LocalDateTime>();

        HourlySpliterator.of(LocalDateTime.parse("2007-12-03T10:15"), LocalDateTime.parse("2007-12-05T10:15"))
        .forEach(dates::add); 

        System.out.println(dates); 


}
Ritesh
  • 7,472
  • 2
  • 39
  • 43