From eaadcf96e2b0d2ff4a518d85a7ae78848cf77a4a Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Mon, 22 Oct 2018 16:36:27 +0200 Subject: [PATCH] BAEL-2292 --- .../com/baeldung/datetime/DateTimeConfig.java | 29 +++++++++++++++++++ .../baeldung/datetime/DateTimeController.java | 29 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeConfig.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeController.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeConfig.java b/spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeConfig.java new file mode 100644 index 0000000000..8a5d1c71af --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeConfig.java @@ -0,0 +1,29 @@ +package com.baeldung.datetime; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar; +import org.springframework.format.number.NumberFormatAnnotationFormatterFactory; +import org.springframework.format.support.DefaultFormattingConversionService; +import org.springframework.format.support.FormattingConversionService; + +import java.time.format.DateTimeFormatter; + +@Configuration +class DateTimeConfig { + + @Bean + public FormattingConversionService conversionService() { + DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false); + + conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); + + DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); + registrar.setDateFormatter(DateTimeFormatter.ofPattern("dd.MM.yyyy")); + registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")); + registrar.registerFormatters(conversionService); + + return conversionService; + } + +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeController.java b/spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeController.java new file mode 100644 index 0000000000..5741b35530 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/datetime/DateTimeController.java @@ -0,0 +1,29 @@ +package com.baeldung.datetime; + +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.Date; + +@RestController +public class DateTimeController { + + @PostMapping("/date") + public void date(@RequestParam("date") @DateTimeFormat(pattern = "dd.MM.yyyy") Date date) { + // ... + } + + @PostMapping("/localdate") + public void localDate(@RequestParam("localDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate localDate) { + // ... + } + + @PostMapping("/localdatetime") + public void dateTime(@RequestParam("localDateTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime localDateTime) { + // ... + } +} \ No newline at end of file