Processing date and time data is a common requirement when developing Spring Boot applications. Spring Boot provides two annotations@DateTimeFormat
and@JsonFormat
Come and help us deal with these issues. These two annotations are used to parse the date string into a date object and format the date object into a string, respectively. This article will introduce the usage and functions of these two annotations in detail and explain them through example code.
@DateTimeFormat annotation
@DateTimeFormat
is annotations provided by Spring framework for formatting and parsing date and time fields. It is mainly used to parse date strings in request parameters or form data into date objects in Java.
usage
@DateTimeFormat
Can be applied to the following data types:
Common properties of this annotation include:
-
pattern
: A pattern string that specifies the date format, e.g."yyyy-MM-dd"
。 -
iso
: Specify the standard ISO date and time format. The optional value is、
and
.DATE_TIME
。
Example
Suppose we have a REST controller that handles the date:
package ; import ; import ; import ; import ; import ; import ; import ; @RestController public class DateController { @GetMapping("/date") public String handleDate(@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) { return "Parsed date: " + (DateTimeFormatter.ISO_DATE); } @GetMapping("/datetime") public String handleDateTime(@RequestParam("datetime") @DateTimeFormat(iso = .DATE_TIME) LocalDateTime dateTime) { return "Parsed datetime: " + (DateTimeFormatter.ISO_DATE_TIME); } }
In the above example:
-
/date
The endpoint processing format is"yyyy-MM-dd"
string and parse it intoLocalDate
Object. -
/datetime
The endpoint processes and parses the date and time string in ISO standard formatLocalDateTime
Object.
You can test these endpoints via the following URL:
http://localhost:8080/date?date=2023-10-01 http://localhost:8080/datetime?datetime=2023-10-01T10:15:30
@JsonFormat annotation
@JsonFormat
Is annotation provided by the Jackson library for serializing and deserializing date and time fields in JSON data. It can help us format datetime objects to a specific string format, or parse strings in a specific format into datetime objects.
usage
@JsonFormat
Can be applied to fields or methods of a class. Its commonly used properties include:
-
pattern
: A pattern string that specifies the date and time format. -
shape
: Specify the formatting type of date and time. Commonly used values are。
-
timezone
: Specify the time zone.
Example
Suppose we have an entity class that contains date and time fields and use@JsonFormat
Annotation formatting date and time:
package ; import ; import ; import ; public class Event { private String name; @JsonFormat(pattern = "yyyy-MM-dd") private LocalDate date; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "UTC") private LocalDateTime dateTime; // getters and setters public String getName() { return name; } public void setName(String name) { = name; } public LocalDate getDate() { return date; } public void setDate(LocalDate date) { = date; } public LocalDateTime getDateTime() { return dateTime; } public void setDateTime(LocalDateTime dateTime) { = dateTime; } }
Next, we create a REST controller to test the serialization and deserialization of the entity class:
package ; import ; import .*; import ; import ; @RestController public class EventController { @GetMapping("/event") public Event getEvent() { Event event = new Event(); ("Spring Boot Workshop"); ((2023, 10, 1)); ((2023, 10, 1, 10, 15, 30)); return event; } @PostMapping("/event") public String createEvent(@RequestBody Event event) { // Persistence logic... return "Event created: " + (); } }
In the above example:
-
/event
The GET endpoint returns aEvent
object, where date and date time fields will be based on@JsonFormat
The format specified by the annotation is serialized to a JSON string. -
/event
The POST endpoint accepts a JSON request body and deserializes it toEvent
Object.
You can test these endpoints in the following ways:
GET Request
curl -X GET http://localhost:8080/event
Return result:
{
"name": "Spring Boot Workshop",
"date": "2023-10-01",
"dateTime": "2023-10-01 10:15:30"
}
POST request
curl -X POST http://localhost:8080/event -H "Content-Type: application/json" -d '{ "name": "Spring Boot Workshop", "date": "2023-10-01", "dateTime": "2023-10-01 10:15:30" }'
Return result:
Event created: Spring Boot Workshop
summary
In this article, we introduce in detail the@DateTimeFormat
and@JsonFormat
The usage and function of annotations.@DateTimeFormat
It is mainly used to parse the date string in the request parameter or form data into a date object, and@JsonFormat
This is used to serialize and deserialize the date and time fields in JSON data.
Through these annotations, we can more conveniently process date and time data, ensuring the format consistency of the data when passed between different levels. This is essential for developing high-quality Spring Boot applications
This is the article about the usage and function of @DateTimeFormat and @JsonFormat in Spring Boot. For more related Spring Boot @DateTimeFormat and @JsonFormat, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!