fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.time.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. LocalDateTime startDate = LocalDateTime.parse("2017-03-07T06:30:00");
  14. LocalDateTime endDate = LocalDateTime.parse("2017-03-07T11:35:00");
  15. LocalDateTime current = startDate;
  16. while (current.isBefore(endDate)) {
  17. LocalDateTime nextHour = current.withMinute(0).plusHours(1);
  18. LocalDateTime rangeEnd = nextHour.isBefore(endDate) ? nextHour : endDate;
  19. System.out.printf("%s - %s%n", current, rangeEnd);
  20. current = rangeEnd;
  21. }
  22. }
  23. }
Success #stdin #stdout 0.13s 4386816KB
stdin
Standard input is empty
stdout
2017-03-07T06:30 - 2017-03-07T07:00
2017-03-07T07:00 - 2017-03-07T08:00
2017-03-07T08:00 - 2017-03-07T09:00
2017-03-07T09:00 - 2017-03-07T10:00
2017-03-07T10:00 - 2017-03-07T11:00
2017-03-07T11:00 - 2017-03-07T11:35