fork download
  1. import java.time.LocalDateTime;
  2. import java.time.format.DateTimeFormatter;
  3. import java.util.Locale;
  4.  
  5. public class Main {
  6. public static void main(String[] args) {
  7. String strStartDateTime = "2021-01-01 00:00:00";
  8. String strEndDateTime = "2021-01-01 10:00:00";
  9.  
  10. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH);
  11.  
  12. LocalDateTime ldtStart = LocalDateTime.parse(strStartDateTime, dtf);
  13. LocalDateTime ldtEnd = LocalDateTime.parse(strEndDateTime, dtf);
  14.  
  15. for (LocalDateTime ldt = ldtStart; !ldt.isAfter(ldtEnd); ldt = ldt.plusHours(1)) {
  16. // System.out.println(ldt);
  17.  
  18. // Formatted
  19. System.out.println(ldt.format(dtf));
  20.  
  21. // ...Your logic
  22. }
  23. }
  24. }
Success #stdin #stdout 0.13s 49216KB
stdin
Standard input is empty
stdout
2021-01-01 00:00:00
2021-01-01 01:00:00
2021-01-01 02:00:00
2021-01-01 03:00:00
2021-01-01 04:00:00
2021-01-01 05:00:00
2021-01-01 06:00:00
2021-01-01 07:00:00
2021-01-01 08:00:00
2021-01-01 09:00:00
2021-01-01 10:00:00