欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決springboot 實體類String轉(zhuǎn)Date類型的坑

 更新時間:2021年10月25日 10:32:40   作者:DemonsPan  
這篇文章主要介紹了解決springboot 實體類String轉(zhuǎn)Date類型的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

springboot 實體類String轉(zhuǎn)Date類型

前端傳入一個String的時間字符串如:2019-07-18 23:59:59

后端實體類要在頭頂加注解:

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

不然會出現(xiàn)報錯

Date解析String類型的參數(shù)

1.首先建立String to Date 的解析實現(xiàn)

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateConverter implements Converter<String, Date> {
    private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
    private static final String shortDateFormat = "yyyy-MM-dd";
    @Override
    public Date convert(String value) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        value = value.trim();
        try {
            if (value.contains("-")) {
                SimpleDateFormat formatter;
                if (value.contains(":")) {
                    formatter = new SimpleDateFormat(dateFormat);
                } else {
                    formatter = new SimpleDateFormat(shortDateFormat);
                }
                Date dtDate = formatter.parse(value);
                return dtDate;
            } else if (value.matches("^\\d+$")) {
                Long lDate = new Long(value);
                return new Date(lDate);
            }
        } catch (Exception e) {
            throw new RuntimeException(String.format("parser %s to Date failed", value));
        }
        throw new RuntimeException(String.format("parser %s to Date failed", value));
    }
}

2.創(chuàng)建全局的解析配置

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import javax.annotation.PostConstruct;
@Configuration
public class DateHandlerAdapter {
    @Autowired
    private RequestMappingHandlerAdapter handlerAdapter;
    /**
     * 增加字符串轉(zhuǎn)日期的全局適配器
     */
    @PostConstruct
    public void initEditableAvlidation() {
        ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter
                .getWebBindingInitializer();
        if (initializer.getConversionService() != null) {
            GenericConversionService genericConversionService = (GenericConversionService) initializer
                    .getConversionService();
            genericConversionService.addConverter(new StringToDateConverter());
        }
    }
}

添加完這兩個文件以后 在傳參數(shù)類型為Date的參數(shù)時就不會再報 date解析失敗的錯誤了。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot yml配置文件定義list集合、數(shù)組和map以及使用中的錯誤

    springboot yml配置文件定義list集合、數(shù)組和map以及使用中的錯誤

    這篇文章主要介紹了springboot yml配置文件定義list集合、數(shù)組和map以及使用中遇到的錯誤問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java中線程的基本方法使用技巧

    Java中線程的基本方法使用技巧

    這篇文章主要介紹了Java中線程的基本方法使用技巧,需要的朋友可以參考下
    2017-09-09
  • GC參考手冊二java中垃圾回收原理解析

    GC參考手冊二java中垃圾回收原理解析

    由于有個垃圾回收機(jī)制,java中的額對象不在有“作用域”的概念,只有對象的引用才有“作用域”。垃圾回收可以有效的防止內(nèi)存泄露,有效的使用空閑的內(nèi)存<BR>
    2022-01-01
  • Java?C++算法題解leetcode801使序列遞增的最小交換次數(shù)

    Java?C++算法題解leetcode801使序列遞增的最小交換次數(shù)

    這篇文章主要為大家介紹了Java?C++題解leetcode801使序列遞增的最小交換次數(shù)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • 利用Kotlin + Spring Boot實現(xiàn)后端開發(fā)

    利用Kotlin + Spring Boot實現(xiàn)后端開發(fā)

    這篇文章主要給大家介紹了關(guān)于利用Kotlin + Spring Boot實現(xiàn)后端開發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Spring?Security實現(xiàn)用戶名密碼登錄詳解

    Spring?Security實現(xiàn)用戶名密碼登錄詳解

    這篇文章主要為大家詳細(xì)介紹了Spring Security如何實現(xiàn)用戶名密碼登錄功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-10-10
  • 用Java8 stream處理數(shù)據(jù)

    用Java8 stream處理數(shù)據(jù)

    這篇文章主要介紹了用Java8 stream處理數(shù)據(jù),Java 8 API的設(shè)計者重新提出了一個新的抽象稱為流Stream,可以讓我們以一種聲明的方式處理數(shù)據(jù),此外,數(shù)據(jù)流可以充分利用多核架構(gòu)而無需編寫多線程的一行代碼,下面我們一起來看看文章詳細(xì)介紹
    2021-11-11
  • maven坐標(biāo)Dependencies和Exclusions的使用

    maven坐標(biāo)Dependencies和Exclusions的使用

    這篇文章主要介紹了maven坐標(biāo)Dependencies和Exclusions的使用,很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring Data分頁與排序的實現(xiàn)方法

    Spring Data分頁與排序的實現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于Spring Data分頁與排序的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • 學(xué)習(xí)非阻塞的同步機(jī)制CAS

    學(xué)習(xí)非阻塞的同步機(jī)制CAS

    現(xiàn)代的處理器都包含對并發(fā)的支持,其中最通用的方法就是比較并交換(compare and swap),簡稱CAS。下面我們來一起學(xué)習(xí)一下吧
    2019-05-05

最新評論