Spring Boot - Handling Date Request Parameter

Handling Date types in the Spring Boot Controller

Handling Date type

Date can be received as parameterusing long, java.utils.Date, ZonedDateTime and LocalDateTime.
The @DateTimeFormat annotation lets you specify the format with the ISO enum. Alternatively, the format can be specified with the pattern attribute of the annotation.

see DateController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* GET http://localhost:8080/testLong?start=1527814861000 Here we pass number of
* milliseconds since the standard base time known as "the epoch".
* use https://www.epochconverter.com/ to convert to epoch time
*
* @param start
* @return
*/
@RequestMapping("testLong")
public ResponseEntity<Date> testLong(long start) {
Date startDate = new Date(start);
return ResponseEntity.ok().body(startDate);
}

// GET http://localhost:8080/testDate?start=2018-01-01
@RequestMapping("testDate")
public ResponseEntity<Date> testDate(
@RequestParam("start") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date start) {
return ResponseEntity.ok().body(start);
}

// GET http://localhost:8080/testDatePattern?start=2018-03EST
// GET http://localhost:8080/testDatePattern?start=2018-03EST
@RequestMapping("testDatePattern")
public ResponseEntity<Date> testDatePattern(
@RequestParam("start") @DateTimeFormat(pattern = "yyyy-MMz") Date start) {
return ResponseEntity.ok().body(start);
}

// GET http://localhost:8080/testDateTime?start=2018-10-31T01:30:00.000-05:00
// GET http://localhost:8080/testDateTime?start=2018-10-31T01:30:00.000Z
@RequestMapping("testDateTime")
public ResponseEntity<Date> testDateTime(
@RequestParam("start") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date start) {
return ResponseEntity.ok().body(start);
}

// GET http://localhost:8080/testZonedDateTime?start=2018-10-31T01:30:00.000Z
// GET http://localhost:8080/testZonedDateTime?start=2018-10-31T01:30:00.000%2B01:00 // 2018-10-31T01:30+01:00
// '+' URL encoded => '%2B'
@RequestMapping("testZonedDateTime")
public ResponseEntity<String> testZonedDateTime(
@RequestParam("start") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime start) {
return ResponseEntity.ok().body(start.toString());
}

// GET http://localhost:8080/testLocalDateTime?start=2018-10-31T01:30:00.000Z
// GET http://localhost:8080/testLocalDateTime?start=2018-10-31T01:30:00.000%2B01:00 // 2018-10-31T01:30
@RequestMapping("testLocalDateTime")
public ResponseEntity<String> testLocalDateTime(
@RequestParam("start") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start) {
return ResponseEntity.ok().body(start.toString());
}

If you have Jackson integeration with your application to serialize your bean to JSON format
then you can use Jackson anotation @JsonFormat to format you date to specified format.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// POST localhost:8080/testDateContainer
// body {"date": "2018-01-01", "dateTime": "2018-07-04T12:08:56.235-0000"}
@PostMapping("testDateContainer")
public ResponseEntity<String> testDateContainer(@RequestBody DateContainer dataContainer) {
return ResponseEntity.ok().body(dataContainer.toString());
}

@Data
private static class DateContainer {
@JsonFormat(pattern="yyyy-MM-dd")
Date date;

// pattern follows java.text.SimpleDateFormat
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSSZ")
Date dateTime;
}

Reference

Sourcecode - SpringBootExamples - web-application