Spring Controller Handling Date Parameters

Spring Rest Controller handling Dates

Spring’s @DateTimeFormat Annotation

Handling Date Request Parameters

Use Spring’s org.springframework.format.annotation.DateTimeFormat to specify the date format for the request parameter.

1
2
3
4
5
6
7
8
9
@RestController
public class MyController {

// http://localhost:8080/api/data?date=2021-12-31
@GetMapping("/api/data")
public String getData(@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
return "Date: " + date;
}
}

use iso attribut to specify the date format for the request parameter.

1
2
3
4
5
6
7
8
9
@RestController
public class MyController {

// http://localhost:8080/api/data?date=2021-12-31
@GetMapping("/api/data")
public String getData(@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
return "Date: " + date;
}
}

Handling DateTime Request Parameters

Use Spring’s org.springframework.format.annotation.DateTimeFormat to specify the date format for the request parameter.

1
2
3
4
5
6
7
8
9
10
@RestController
public class MyController {

// http://localhost:8080/api/data?date=2021-12-31T01:30:00Z
// http://localhost:8080/api/data?date=2024-10-31T01:30:00.000-05:00
@GetMapping("/api/data")
public String getData(@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) ZonedDateTime dateTime) {
return "Date: " + dateTime;
}
}

For LocalDateTime

1
2
3
4
5
6
7
8
9
@RestController
public class MyController {

// http://localhost:8080/api/data?date=2021-12-31T01:30:00
@GetMapping("/api/data")
public String getData(@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateTime) {
return "Date: " + dateTime;
}
}

Handling Time Request Parameters

Use Spring’s org.springframework.format.annotation.DateTimeFormat to specify the date format for the request parameter.

1
2
3
4
5
6
7
8
9
@RestController
public class MyController {

// http://localhost:8080/api/data?time=01:30:00
@GetMapping("/api/data")
public String getData(@RequestParam("time") @DateTimeFormat(pattern = "HH:mm:ss") LocalTime time) {
return "Time: " + time;
}
}

Epoch time in Request parameters

You can also pass time in epoch time format and convert it to ZonedDateTime.

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
public class MyController {

// http://localhost:8080/api/data?time=1734194291
// output: Date: 2024-12-14T11:38:11-05:00[America/New_York]
@GetMapping("/api/data")
public String getData(@RequestParam("time") long time) {
ZonedDateTime zonedDateTime = Instant.ofEpochSecond(time)
.atZone(ZoneId.of("America/New_York"));
return "Date: " + zonedDateTime;
}
}

You can also pass time in epoch millisecond format and convert it to ZonedDateTime.

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
public class MyController {

// http://localhost:8080/api/data?time=1734194291000
// output: Date: 2024-12-14T11:38:11-05:00[America/New_York]
@GetMapping("/api/data")
public String getData(@RequestParam("time") long time) {
ZonedDateTime zonedDateTime = Instant.ofEpochMilli(time)
.atZone(ZoneId.of("America/New_York"));
return "Date: " + zonedDateTime;
}
}