File tree Expand file tree Collapse file tree
spring-mvc-java/src/main/java/com/baeldung/datetime Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .datetime ;
2+
3+ import org .springframework .context .annotation .Bean ;
4+ import org .springframework .context .annotation .Configuration ;
5+ import org .springframework .format .datetime .standard .DateTimeFormatterRegistrar ;
6+ import org .springframework .format .number .NumberFormatAnnotationFormatterFactory ;
7+ import org .springframework .format .support .DefaultFormattingConversionService ;
8+ import org .springframework .format .support .FormattingConversionService ;
9+
10+ import java .time .format .DateTimeFormatter ;
11+
12+ @ Configuration
13+ class DateTimeConfig {
14+
15+ @ Bean
16+ public FormattingConversionService conversionService () {
17+ DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService (false );
18+
19+ conversionService .addFormatterForFieldAnnotation (new NumberFormatAnnotationFormatterFactory ());
20+
21+ DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar ();
22+ registrar .setDateFormatter (DateTimeFormatter .ofPattern ("dd.MM.yyyy" ));
23+ registrar .setDateTimeFormatter (DateTimeFormatter .ofPattern ("dd.MM.yyyy HH:mm:ss" ));
24+ registrar .registerFormatters (conversionService );
25+
26+ return conversionService ;
27+ }
28+
29+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .datetime ;
2+
3+ import org .springframework .format .annotation .DateTimeFormat ;
4+ import org .springframework .web .bind .annotation .PostMapping ;
5+ import org .springframework .web .bind .annotation .RequestParam ;
6+ import org .springframework .web .bind .annotation .RestController ;
7+
8+ import java .time .LocalDate ;
9+ import java .time .LocalDateTime ;
10+ import java .util .Date ;
11+
12+ @ RestController
13+ public class DateTimeController {
14+
15+ @ PostMapping ("/date" )
16+ public void date (@ RequestParam ("date" ) @ DateTimeFormat (pattern = "dd.MM.yyyy" ) Date date ) {
17+ // ...
18+ }
19+
20+ @ PostMapping ("/localdate" )
21+ public void localDate (@ RequestParam ("localDate" ) @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE ) LocalDate localDate ) {
22+ // ...
23+ }
24+
25+ @ PostMapping ("/localdatetime" )
26+ public void dateTime (@ RequestParam ("localDateTime" ) @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) LocalDateTime localDateTime ) {
27+ // ...
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments