在SpringBoot中配置日期格式化的方法詳解
我們先了解下,為什么需要配置日期格式化?
通常情況下,發(fā)起一個(gè) Http 請求,Spring Boot 會根據(jù)請求路徑映射到指定 Controller 上的某個(gè)方法的參數(shù)上,接著,Spring 會自動進(jìn)行類型轉(zhuǎn)換。
對于日期類型的參數(shù),Spring 默認(rèn)是沒有配置如何將字符串轉(zhuǎn)換成日期類型的
未配置日期格式化會如何?
我們新建一個(gè) Web 項(xiàng)目,并定義一個(gè)接口:
package site.exception.springbootdateformat.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import site.exception.springbootdateformat.entity.User;
/**
* @time 21:03
* @discription
**/
@RestController
public class UserController {
/**
* 定義一個(gè)創(chuàng)建用戶的接口
* @param user
* @return
*/
@PostMapping("/user")
public String createUser(User user) {
// 打印創(chuàng)建時(shí)間
System.out.println(user.getCreateTime().toString());
return "Create user success !";
}
}
User.java:
package site.exception.springbootdateformat.entity;
import java.io.Serializable;
import java.util.Date;
/**
* @time 21:00
* @discription
**/
public class User implements Serializable {
/**
* 用戶名
*/
private String username;
/**
* 密碼
*/
private String password;
/**
* 創(chuàng)建時(shí)間
*/
private Date createTime;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
用 Postman 工具發(fā)起對該接口的表單請求,看看結(jié)果:

服務(wù)報(bào)錯(cuò),無法將字符串類型轉(zhuǎn)換成 java.util.Date 類型。
配置日期格式化
要讓 Spring Boot 能夠按照指定的格式進(jìn)行日期類型轉(zhuǎn)換,需要做以下步驟:
- 定義一個(gè)
MvcConfig類,讓其實(shí)現(xiàn)WebMvcConfigurer接口; - 重寫
addFormatters方法; - 添加一個(gè)
DateFormatter;
package site.exception.springbootdateformat.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author www.exception.site (exception 教程網(wǎng))
* @date 2019/2/16
* @time 20:55
* @discription
**/
@Configuration
public class MvcConfig implements WebMvcConfigurer {
/**
* 配置日期格式化
* @param registry
*/
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatter(new DateFormatter("yyyy-MM-dd HH:mm:ss"));
}
}
接下來,再次以表單的形式訪問 /user 接口,看看效果:

正確輸出 Create user Success ! 接口,并且控制臺,也打印了 createTime 值,說明 Spring Boot 已經(jīng)自動幫我們做了轉(zhuǎn)換操作:

至此,如何在 Spring Boot 中配置全局的日期格式化就完成了!
注意:本節(jié)中的轉(zhuǎn)換僅支持表單形式請求,不支持
application/json的請求方式!
以上就是在SpringBoot中配置日期格式化的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot日期格式化的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決Java壓縮zip異常java.util.zip.ZipException:duplicate entry
這篇文章主要介紹了解決Java壓縮zip異常java.util.zip.ZipException:duplicate entry:問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Java根據(jù)URL下載文件到本地的2種方式(大型文件與小型文件)
這篇文章主要給大家介紹了關(guān)于Java根據(jù)URL下載文件到本地的2種方式,分別是大型文件與小型文件,避免內(nèi)存溢出OOM,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
springboot+zookeeper實(shí)現(xiàn)分布式鎖的示例代碼
本文主要介紹了springboot+zookeeper實(shí)現(xiàn)分布式鎖的示例代碼,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
零基礎(chǔ)搭建boot+MybatisPlus的詳細(xì)教程
這篇文章主要介紹了零基礎(chǔ)搭建boot+MybatisPlus,首先需要創(chuàng)建數(shù)據(jù)庫表和創(chuàng)建boot項(xiàng)目使用mybatisplus操作數(shù)據(jù)庫,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
spring mvc實(shí)現(xiàn)登錄賬號單瀏覽器登錄
這篇文章主要為大家詳細(xì)介紹了spring mvc實(shí)現(xiàn)登錄賬號單瀏覽器登錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04

