In the tutorial, we show how to work with DateTime request param in SpringBoot RestAPI using @DateTimeFormat
.
@DateTimeFormat with DateTime Request Param
@DateTimeFormat
is used to declare a field or method parameter should be formatted as a date or time.
We can use @DateTimeFormat
with ISO date time pattern, or custom format pattern string:
– Common ISO enum value: DATE
, TIME
, DATE_TIME
- DATE:
yyyy-MM-dd
, example2019-03-28
- TIME:
HH:mm:ss.SSSXXX
, example01:30:00.000-05:00
- DATE_TIME:
yyyy-MM-dd'T'HH:mm:ss.SSSXXX
, example2019-03-28T01:30:00.000+07:00
Example @DateTimeFormat
with @RequestParam
in Spring RestAPI:
@GetMapping("/date/v1") public String dateTimeApiV1( @RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date, @RequestParam("localdate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate localdate, @RequestParam("localdatetime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime localdatetime) { |
– Custom format pattern string:
Example:
@GetMapping("/date/v2") public String dateTimeApiV2( @RequestParam("date") @DateTimeFormat(pattern="yyyy.MM.dd") Date date, @RequestParam("localdate") @DateTimeFormat(pattern="yyyy.MM.dd") LocalDate localdate, @RequestParam("localdatetime") @DateTimeFormat(pattern="yyyy.MM.dd HH:mm:ss") LocalDateTime localdatetime) { |
Practice
Create SpringBoot project
We create a SpringBoot project as below:
– Dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
Create DateTime RestAPIs
– RestAPIs.java
:
package com.grokonez.springboot.controller; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class RestAPIs { @GetMapping("/date/v1") public String dateTimeApiV1( @RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date, @RequestParam("localdate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate localdate, @RequestParam("localdatetime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime localdatetime) { System.out.println(date); System.out.println(localdate); System.out.println(localdatetime); return "Done"; } @GetMapping("/date/v2") public String dateTimeApiV2( @RequestParam("date") @DateTimeFormat(pattern="yyyy.MM.dd") Date date, @RequestParam("localdate") @DateTimeFormat(pattern="yyyy.MM.dd") LocalDate localdate, @RequestParam("localdatetime") @DateTimeFormat(pattern="yyyy.MM.dd HH:mm:ss") LocalDateTime localdatetime) { System.out.println(date); System.out.println(localdate); System.out.println(localdatetime); return "Done"; } } |
Run & Check Results
Run SpringBoot project ->
– Request 1 with ISO format pattern:
-> Logs in SpringBoot app:
Fri Aug 23 07:00:00 GMT+07:00 2019 2019-09-12 2019-10-29T01:30 |
– Request 2 with Custom format pattern:
-> Logs in SpringBoot app:
Fri Aug 23 00:00:00 GMT+07:00 2019 2019-09-12 2019-10-29T12:01:23 |
Sourcecode
Conclusion
We had learned how to use @DateTimeFormat
to format Date Time request param in SpringBoot RestAPI with:
- ISO date time pattern
- Custom date time format pattern string
Happy Learning! See you later!