# Given A Formatted Range Of DateTime, Split And Print Every Hour Off Of This Range

## 08 May 2018 - 1 answer

The goal of the Algorithm is to split a range of time into separate dates.

I have a variable: `TimeInterval = 2018-05-03T04:00Z/2018-05-03T06:00Z` Which is a range of 2 hours.

``````String[] All_Time = TimeInterval.split("/");
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'");
LocalDateTime datetimestart = LocalDateTime.parse(All_Time[0], dateFormat);
LocalDateTime datetimeend = LocalDateTime.parse(All_Time[1], dateFormat);
do {
id = datetimestart.toString();
System.out.println(id+"/"+datetimestart.plusHours(1).toString());
datetimestart = datetimestart.plusHours(1);
} while (datetimestart.plusHours(1).equals(datetimeend));
System.out.println("Time End: "+datetimeend);
``````

This prints:

``````2018-05-03T04:00/2018-05-03T05:00
2018-05-03T05:00/2018-05-03T06:00
Time End: 2018-05-03T06:00
``````

Which is exactly what I want (though I notice the 'Z' is missing...).

But, when the range of time is of 24 Hours (And here I must also check the day of 23 and 25 Hours), the loop breaks and given this `TimeInterval = 2018-05-03T04:00Z/2018-05-04T04:00Z` The output is:

``````2018-05-03T08:00/2018-05-03T09:00
Time End: 2018-05-04T04:00
``````

I know it is a problem with the `plusHours(1)` that's not able to add +1 to the day, and possibly the same happens when going from the 31st (28,29,30th day of the month) to the 1st of next month --> ... And same for the year.

Is there a solution for this?

UPDATE:

``````for (int inc=0; inc<=24; inc++){
System.out.println("Hour: "+datetimestart.plusHours(inc));
}
``````

Prints:

``````Hour: 2018-05-03T08:00
Hour: 2018-05-03T09:00
Hour: 2018-05-03T10:00
Hour: 2018-05-03T11:00
Hour: 2018-05-03T12:00
Hour: 2018-05-03T13:00
Hour: 2018-05-03T14:00
Hour: 2018-05-03T15:00
Hour: 2018-05-03T16:00
Hour: 2018-05-03T17:00
Hour: 2018-05-03T18:00
Hour: 2018-05-03T19:00
Hour: 2018-05-03T20:00
Hour: 2018-05-03T21:00
Hour: 2018-05-03T22:00
Hour: 2018-05-03T23:00
Hour: 2018-05-04T00:00
Hour: 2018-05-04T01:00
Hour: 2018-05-04T02:00
Hour: 2018-05-04T03:00
Hour: 2018-05-04T04:00
Hour: 2018-05-04T05:00
Hour: 2018-05-04T06:00
Hour: 2018-05-04T07:00
Hour: 2018-05-04T08:00
``````

It means to me that there are good chances that the do-while loop might be not the best here, I am not sure why tho.

``````    String timeInterval = "2018-05-03T08:00Z/2018-05-04T04:00Z";
String[] allTime = timeInterval.split("/");
if (allTime.length != 2) {
throw new IllegalStateException("Wrong number of datetimes, should be 2");
}
OffsetDateTime dateTimeStart = OffsetDateTime.parse(allTime[0]);
OffsetDateTime dateTimeEnd = OffsetDateTime.parse(allTime[1]);
OffsetDateTime current = dateTimeStart;
while (current.isBefore(dateTimeEnd)) {
OffsetDateTime next = current.plusHours(1);
System.out.println("" + current + '/' + next);
current = next;
}
System.out.println("Time End: " + dateTimeEnd);
``````

Output (abbreviated in the middle):

``````2018-05-03T08:00Z/2018-05-03T09:00Z
2018-05-03T09:00Z/2018-05-03T10:00Z
2018-05-03T10:00Z/2018-05-03T11:00Z
…
2018-05-03T22:00Z/2018-05-03T23:00Z
2018-05-03T23:00Z/2018-05-04T00:00Z
2018-05-04T00:00Z/2018-05-04T01:00Z
2018-05-04T01:00Z/2018-05-04T02:00Z
2018-05-04T02:00Z/2018-05-04T03:00Z
2018-05-04T03:00Z/2018-05-04T04:00Z
Time End: 2018-05-04T04:00Z
``````

Your `while` condition was wrong. You used `equals`, which made sure that you only took a second time through the loop if the end of the second hour coincided with your end time. And never a third time. Instead use `isBefore` as in my code. Another option is `! datetimestart.isEqual(datetimeend)` (the exclamation mark means “not”). I am assuming that the interval is always a whole number of hours and that start and end are at the same UTC offset (`Z` means offset zero). Otherwise it’ll be a bit more complicated.

Other points to note:

• Use the naming conventions (at least when sharing your code on Stack Overflow and elsewhere). Variable names begin with a lowercase letter and doesn’t contain underscores.
• To preserve the `Z`, use `OffsetDateTime`, not `LocalDateTime`. Since your start and end time strings are in the standard format of ISO 8601, you don’t need a `DateTimeFormatter`in your case.