본문 바로가기
SpringBoot

LocalDateTime hour step

by ByteBridge 2021. 10. 31.
반응형
//1. step 1 hour
var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime start = LocalDateTime.parse("2021-10-30 00:00:00", formatter), end = LocalDateTime.parse("2021-10-31 00:00:00",formatter);
    Stream.iterate(start, dt->dt.plusHours(1))
        .limit(ChronoUnit.HOURS.between(start, end) + 1)
        .forEach(System.out::println);

//2. step 1 hour
var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime start = LocalDateTime.parse("2021-10-30 00:00:00", formatter), end = LocalDateTime.parse("2021-10-31 00:00:00",formatter);
    LocalDateTime next = start.minusHours(1);
    while((next = next.plusHours(1)).isBefore(end.plusHours(1))) {
      System.out.println(next);
    }

//output (1,2 same result)
2021-10-30T00:00
2021-10-30T01:00
2021-10-30T02:00
...
2021-10-30T22:00
2021-10-30T23:00
2021-10-31T00:00
반응형

'SpringBoot' 카테고리의 다른 글

java excell downloader in spring boot  (0) 2021.10.31
Java conver t JsonArray to List  (0) 2021.10.31
ModelMapper 를 사용하여 객체 컨버팅 하기  (0) 2021.05.23
Spring AOP  (0) 2020.12.06
spring factory pattern  (0) 2020.10.19