原文地址:
https://stackoverflow.com/questions/29196466/convert-unix-timestamp-to-java-date-spring-requestparam
Using @InitBinder and WebDataBinder:
@RestController
public class SimpleController {
//... your handlers here...
@InitBinder
public void initBinder(final WebDataBinder webdataBinder) {
webdataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(new Date(Long.valueOf(text)));
}
});
}
}
另外一种方法:
原文网址:
https://www.baeldung.com/spring-mvc-custom-data-binder
@Component
public class StringToLocalDateTimeConverter
implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String source) {
return LocalDateTime.parse(
source, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
}
@GetMapping("/findbydate/{date}")
public GenericEntity findByDate(@PathVariable("date") LocalDateTime date) {
return ...;
}