날짜 정보를 LocalDate 로 받기

in java •  7 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
}
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!