In Spring Boot, you can use the DateTimeFormat annotation to specify the format of a date-time field in a Java bean. The DateTimeFormat annotation is part of the org.springframework.format.annotation package.
Here’s an example of how you can use the DateTimeFormat annotation to format a date-time field in a Java bean:
import org.springframework.format.annotation.DateTimeFormat;
public class Event {
private String name;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime startTime;
// ...
}
In this example, we have defined an Event class with a name field and a startTime field of type LocalDateTime. We have used the DateTimeFormat annotation to specify that the startTime field should be formatted as a string in the “yyyy-MM-dd HH:mm:ss” format when it is converted to or from a string.
You can use the DateTimeFormat annotation with any field of a Java bean that is of a date-time type, such as LocalDateTime, ZonedDateTime, Instant, or Date. You can also use the NumberFormat annotation to format fields of numeric types, and the JsonFormat annotation to format fields of JSON objects.
Spring Boot also provides a set of default formats for date-time fields, which you can override by specifying the pattern attribute of the DateTimeFormat annotation. For example, you can use the iso pattern to format a date-time field in the ISO-8601 format, or the dd/MM/yyyy HH:mm:ss pattern to format a date-time field in the “dd/MM/yyyy HH:mm:ss” format.
@DateTimeFormat(pattern = "iso")
private LocalDateTime startTime;
@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm:ss")
private LocalDateTime endTime;
datetimeformat spring boot example with kotlin
In Spring Boot, you can use the DateTimeFormat annotation to specify the format of a date-time field in a Kotlin data class. The DateTimeFormat annotation is part of the org.springframework.format.annotation package.
Here’s an example of how you can use the DateTimeFormat annotation to format a date-time field in a Kotlin data class:
import org.springframework.format.annotation.DateTimeFormat
data class Event(
val name: String,
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
val startTime: LocalDateTime
)
In this example, we have defined an Event data class with a name field and a startTime field of type LocalDateTime. We have used the DateTimeFormat annotation to specify that the startTime field should be formatted as a string in the “yyyy-MM-dd HH:mm:ss” format when it is converted to or from a string.
You can use the DateTimeFormat annotation with any field of a Kotlin data class that is of a date-time type, such as LocalDateTime, ZonedDateTime, Instant, or Date. You can also use the NumberFormat annotation to format fields of numeric types, and the JsonFormat annotation to format fields of JSON objects.
Spring Boot also provides a set of default formats for date-time fields, which you can override by specifying the pattern attribute of the DateTimeFormat annotation. For example, you can use the iso pattern to format a date-time field in the ISO-8601 format, or the dd/MM/yyyy HH:mm:ss pattern to format a date-time field in the “dd/MM/yyyy HH:mm:ss” format.
@DateTimeFormat(pattern = "iso")
val startTime: LocalDateTime
@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm:ss")
val endTime: LocalDateTime
@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
, example 2019-03-28
- TIME:
HH:mm:ss.SSSXXX
, example 01:30:00.000-05:00
- DATE_TIME:
yyyy-MM-dd'T'HH:mm:ss.SSSXXX
, example 2019-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:
org.springframework.boot
spring-boot-starter-web
Create DateTime RestAPIs
– RestAPIs.java
:
package com.ozenero.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
SpringDateTimeFormatExamples
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!