Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date format in the description #12519

Open
alexp0610 opened this issue Feb 5, 2025 · 1 comment
Open

Date format in the description #12519

alexp0610 opened this issue Feb 5, 2025 · 1 comment

Comments

@alexp0610
Copy link

alexp0610 commented Feb 5, 2025

Hello.

I need to change the date format in the UI description from "yyyy-MM-dd" to "dd/MM/yyyy".
Could anyone help me?

UI Example:

"finalDate": "01/02/2025",

Not work:

@DateTimeFormat(pattern = "dd/MM/yyyy")

Java property:

@Parameter private LocalDate finalDate;

Swagger v3:

<dependency> <groupId>io.swagger.core.v3</groupId> <artifactId>swagger-annotations</artifactId> <version>2.2.21</version> </dependency>

@Assenav-Gnuj
Copy link

The key point is that the @DateTimeFormat annotation only affects how Spring MVC binds incoming request parameters—it doesn’t change the serialization of dates in your generated Swagger documentation. To update the UI description for a LocalDate property, you need to control the serialization process that Swagger (via Jackson) uses.

There are two common approaches:

1. Annotate the Field:

Use the Jackson @jsonformat annotation along with the Swagger @Schema annotation. For example:

@Schema(type = "string", pattern = "dd/MM/yyyy", example = "01/02/2025") @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy") private LocalDate finalDate;
This tells Jackson to serialize the LocalDate using the "dd/MM/yyyy" pattern and informs Swagger of the intended format.

2. Configure the ObjectMapper Globally:

If you prefer a global solution (for all LocalDate fields), you can customize your Jackson ObjectMapper. In a Spring Boot application, for example, you can register a JavaTimeModule and set the date format:

@Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule()); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); mapper.setDateFormat(new SimpleDateFormat("dd/MM/yyyy")); return mapper; }
This approach ensures that all LocalDate values are serialized in the desired format across your API documentation.

Using either approach will adjust the date format in the Swagger UI description from "yyyy-MM-dd" to "dd/MM/yyyy".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants