날짜 정보를 LocalDate 로 받기

in #java7 years ago (edited)

날짜 정보 (yyyy-MM-dd) 를 아래와 같이 처리하고 있었습니다.

@RequestMapping(...)
public void read (
        @RequestParam(value = "start", required = false)
        final String start_,
        @RequestParam(value = "end", required = false)
        final String end_) {
    final LocalDate start;
    try {
        start = ofNullable(start_)
                .map(LocalDate::parse).orElse(null);
    } catch (final DateTimeParseException dtpe) {
        return ...
    }
    final LocalDate end;
    try {
        end = ofNullable(end_)
                .map(LocalDate::parse).orElse(null);
    } catch (final DateTimeParseException dtpe) {
        return ...
    }
    // @todo: do something with start and end
}

Parsing of LocalDate query parameters in Spring Boot 를 보니 곧바로 LocalDate 를 사용할 수 있더군요.
다음과 같이 짧아졌습니다.

@RequestMapping(...)
public void doSome(
        @RequestParam(value = "start", required = false)
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
        final LocalDate start,
        @RequestParam(value = "end", required = false)
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
        final LocalDate end) {
    // @todo: do some with start and end
}

Coin Marketplace

STEEM 0.17
TRX 0.16
JST 0.029
BTC 60988.21
ETH 2373.39
USDT 1.00
SBD 2.55