SpringBoot處理前端日期格式


前端傳到后台的日期格式是字符串類型的。

而java代碼中的類型是Date,這個時候往往會出現類型轉換的問題。

Spring中有Converter接口可以進行類型轉換。

以下是在SpringBoot中使用Converter進行轉換。

 

@Component
public class StringToDateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String source) {
        if (source.length() == 0) {
            return null;
        }
        // yyyy-MM-dd 10
        // yyyy-MM-dd HH:mm:ss 19
        if(source.length() == 10) {
            return DateUtil.parseStrToDate(source, "yyyy-MM-dd");
        } else if(source.length() == 19) {
            return DateUtil.parseStrToDate(source, "yyyy-MM-dd HH:mm:ss");
        }
        return null;
    }

}

更多的日期格式,可以做更多的處理。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM