0

I need create dates from range.

Example:

Start: 01.01.2017 16:30
End:   04.01.2017 23.30

Expected result:

01.01.2017 16:30
01.01.2017 23:00
01.02.2017 09:00
01.02.2017 23:00
01.03.2017 09:00
01.03.2017 23:00
01.04.2017 09:00
01.04.2017 23:00
01.04.2017 23.30
etc...

Is there any better way?

ZonedDateTime start = ZonedDateTime.now();
ZonedDateTime end = ZonedDateTime.now().plusDays(10);

List<ZonedDateTime> result = new ArrayList();
result.add(start);

while(start.isBefore(end) || start.compareTo(end)==0){
  if(start.getHour == 23 || start.getMinute() == 0){
     result.add(start);
  }
  if(start.getHour == 9 || start.getMinute() == 0){
     result.add(start);
  }
  start = start.addMinutes(1);
}
result.add(end);
ernest_k
  • 44,416
  • 5
  • 53
  • 99
gcmarcin
  • 17
  • 4
  • 1
    What is time rules? – LHA Aug 23 '18 at 21:27
  • What's the pattern? – shmosel Aug 23 '18 at 21:30
  • Just add the right number of hours - the pattern is obvious. First work out the difference between how and the first interval. Then just move the right number of hours each time, until you hit 10 days from the start. Will be **much** faster than iterating minute by minute for 10 days (10 * 365 * 60 iterations). – Boris the Spider Aug 23 '18 at 21:41
  • 1
    Got to say that your description doesn't match your expected output, for me anyway, so you just want to add `16:30` and `23.30` to a `List` for a given number of days? – MadProgrammer Aug 23 '18 at 22:29

2 Answers2

1

So, you say you want to iterate time between two dates, and based on your expected output, you only want two specific times for each day, which raises questions over why you're incrementing by minutes.

Maybe (conceptually) something more like...

String startValue = "01.01.2017 16:30";
String endValue = "04.01.2017 23:30";

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
        .appendPattern("dd.MM.yyyy HH:mm")
        .toFormatter(Locale.UK);

LocalDateTime startDate = LocalDateTime.parse(startValue, formatter);
LocalDateTime endTime = LocalDateTime.parse(endValue, formatter);

List<LocalDateTime> times = new ArrayList<>(10);
for (LocalDateTime time = startDate; time.isBefore(endTime); time = time.plusDays(1)) {
    times.add(time.withHour(16).withMinute(30));
    times.add(time.withHour(23).withMinute(00));
}

for (LocalDateTime zdt : times) {
    System.out.println(formatter.format(zdt));
}

would help resolve the issue.

This outputs...

01.01.2017 16:30
01.01.2017 23:00
02.01.2017 16:30
02.01.2017 23:00
03.01.2017 16:30
03.01.2017 23:00
04.01.2017 16:30
04.01.2017 23:00

Other solution might be to have two anchor times, one for 16:30 and one for 23:00 and simply increment them by a day on each loop

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-1

You can use Duration to achieve this. This is a class which provide actions on LocalTime, ZonedDateTime or LocalDateTime objects.

Examples :

Duration daily = Duration.ofDays(1);
Duration hourly = Duration.ofHours(1);

With a little algorithm and this class, you should accomplish your purpose.

Algorithm example, modify it for your needs : start to define if you are before 9:00 or before 23:00, create your first ZonedDateTime on the nearest, create a Duration going from 9:00 to 23 and a second going to 23:00 to 9:00, and iterate until you reach your end date, creating ZonedDateTime objects at each iteration.

Emmanuel H
  • 82
  • 8