我們在使用SpringMVC從前端接受傳遞過來的日期數據時,預設傳遞過來的數據是String類型,如果我們從前端傳遞過來的數據格式是yyyy/MM/dd,SpringMVC有內置類型轉化器會將String類型自動轉化成Date類型。但如果我們從前端傳遞過來的數據格式是yyyy-MM-dd,Sprin... ...
引言
我們在使用SpringMVC從前端接受傳遞過來的日期數據時,預設傳遞過來的數據是String類型,如果我們從前端傳遞過來的數據格式是yyyy/MM/dd,SpringMVC有內置類型轉化器會將String類型自動轉化成Date類型。但如果我們從前端傳遞過來的數據格式是yyyy-MM-dd,SpringMVC的內置轉化器就不能自動轉化了,就得我們自定義一個類型轉化器。
註意:如果我們自定義類型轉化器之後,SpringMVC支持內置類型轉化器就不會在生效了。
創建自定義字元串類型轉日期類型的類型轉化器類
package com.qrj.converter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
/**
* @author: qrj
* @description 自定義字元串類型轉日期類型的類型轉換器
* @Date: 2023/9/29 16:41
*/
public class StringToDateConverter implements Converter<String, Date> {
@Override
public Date convert(String s) {
Date date = null;
try{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
date = simpleDateFormat.parse(s);
}catch (Exception e){
e.printStackTrace();
}
return date;
}
}
我們一個在類型轉化器類裡面寫一個main方法進行測試,看能不能將yyyy-MM-dd格式的字元串轉化成Date類型的數據。
public static void main(String[] args) {
//創建類型轉換服務實例
GenericConversionService genericConversionService = new GenericConversionService();
//想類型轉換服務實例中註冊自定義類型轉換器
genericConversionService.addConverter(new StringToDateConverter());
//使用自定義類型轉換器
String dateStr = "2023-9-29";
Date date = genericConversionService.convert(dateStr,Date.class);
System.out.println(date);
}
測試結果如下:
測試成功,自定義類型轉化器可以將yyyy-MM-dd格式的字元串轉化成Date類型的數據。
但問題來咯!有自定義類型轉化器之後,SpringMVC支持內置類型轉化器就不會在生效。如果我們前端數據傳一個yyyy/MM/dd格式的日期呢???
解決這個問題,我們是不是應該進行一個判斷,這裡正則表達式就出場咯。如果前端傳遞過來的是yyyy-MM-dd格式的字元串,就將它轉化為Date類型,如果前端傳遞過來的是yyyy/MM/dd格式的字元串,也將它轉化為Date類型。
用正則表達式判斷日期字元串是否合法
package com.qrj.converter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
/**
* @author: qrj
* @description 自定義字元串類型轉日期類型的類型轉換器
* @Date: 2023/9/29 16:41
*/
public class StringToDateConverter implements Converter<String, Date> {
@Override
public Date convert(String s) {
Date date = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try{
//可以使用正則表達式判斷日期字元串格式是否合法(支持 yyyy-MM-dd 和 yyyy/MM/dd 格式)
boolean isMate1 = Pattern.matches("^(\\d{4})-(\\d{2})-(\\d{2})$",s);
if (isMate1 == true){
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
}
boolean isMate2 = Pattern.matches("^(\\d{4})/(\\d{2})/(\\d{2})$",s);
if (isMate2 == true){
simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
}
date = simpleDateFormat.parse(s);
}catch (Exception e){
e.printStackTrace();
}
return date;
}
}
註意:上面Pattern.matches()方法是Java中用於檢查給定字元串是否與給定模式匹配的靜態方法。它接受兩個參數:第一個參數是表示正則表達式模式的字元串,第二個參數是待檢查的字元串。
編寫測試代碼,進行測試:
//測試自定義類型轉換器
public static void main(String[] args) {
//創建類型轉換服務實例
GenericConversionService genericConversionService = new GenericConversionService();
//想類型轉換服務實例中註冊自定義類型轉換器
genericConversionService.addConverter(new StringToDateConverter());
//使用自定義類型轉換器
String dateStr = "2023/9/29";
Date date = genericConversionService.convert(dateStr,Date.class);
System.out.println(date);
}
測試結果如下:
報錯原因是無法解析的date :"2023/9/29"
遇到問題
在網上查詢資料得知,原因是在 SimpleDateFormat中使用的日期格式與實際的日期字元串格式不匹配,導致無法解析。在"2023/9/29"這種格式的日期字元串中,月份部分只有一位數字,日期部分有兩位數字,而在正則表達式中(\\d{2})日期和月份部分匹配兩位數字。這使得2023/9/29與正則表達式不匹配,返回false,無法將2023/9/29這種格式的日期字元串解析。
解決問題
為瞭解決這一問題,可以通過修改正則表達式的月份跟日期部分為(\\d{1,2})使其匹配一到兩位數字。以下是修改後的代碼:
package com.qrj.converter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
/**
* @author: qrj
* @description 自定義字元串類型轉日期類型的類型轉換器
* @Date: 2023/9/29 16:41
*/
public class StringToDateConverter implements Converter<String, Date> {
@Override
public Date convert(String s) {
Date date = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try{
//可以使用正則表達式判斷日期字元串格式是否合法(支持 yyyy-MM-dd 和 yyyy/MM/dd 格式)
boolean isMate1 = Pattern.matches("^(\\d{4})-(\\d{2})-(\\d{2})$",s);
if (isMate1){
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
}
boolean isMate2 = Pattern.matches("^(\\d{4})/(\\d{1,2})/(\\d{1,2})$",s);
if (isMate2){
simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
}
date = simpleDateFormat.parse(s);
}catch (Exception e){
e.printStackTrace();
}
return date;
}
//測試自定義類型轉換器
public static void main(String[] args) {
//創建類型轉換服務實例
GenericConversionService genericConversionService = new GenericConversionService();
//想類型轉換服務實例中註冊自定義類型轉換器
genericConversionService.addConverter(new StringToDateConverter());
//使用自定義類型轉換器
String dateStr = "2023/9/29";
String dateStr2 = "2023-9-30";
Date date = genericConversionService.convert(dateStr,Date.class);
Date date2 = genericConversionService.convert(dateStr2,Date.class);
System.out.println(date);
System.out.println(date2);
}
}
進行測試如下:
成功將yyyy-MM-dd格式和yyyy/MM/dd格式的字元串轉化成Date類型的數據。
正則表達式
"^(\\d{4})-(\\d{2})-(\\d{2})$"
"^(\\d{4})/(\\d{1,2})/(\\d{1,2})$"
^:匹配字元串的開頭
(\\d{4}):匹配四位數字,表示年份
-或/:匹配橫杠或斜杠字元
(\\d{1,2}):匹配一到兩位數字,表示月份或日期
$:匹配字元串的結尾