拆分-日期校验修改

This commit is contained in:
liyawei
2022-04-18 16:28:13 +08:00
parent 86e59b9200
commit 0b9afb807d
2 changed files with 74 additions and 17 deletions
@@ -1,15 +1,13 @@
package com.jero.common.util;
import org.springframework.util.StringUtils;
import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.springframework.util.StringUtils;
import java.util.*;
/**
* 类描述:时间操作定义类
@@ -647,7 +645,7 @@ public class DateUtils extends PropertyEditorSupport {
return calendar.get(Calendar.YEAR);
}
/***
* @Description: 判断多个日期是否都符合日期格式
* @Description: 判断多个日期是否都符合日期格式 yyyy/m/d、yyyy-MM-dd、yyyy年MM月dd日
* @Author: yangxuenan
* @Date: 2020/12/31 11:31
* @Param: [str]
@@ -655,19 +653,52 @@ public class DateUtils extends PropertyEditorSupport {
*/
public static boolean isValidDate(String str){
if(org.apache.commons.lang3.StringUtils.isNotBlank(str)){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateRegex = "^\\d{4}年\\d{1,2}月\\d{1,2}日";
str = str.replace(",",",");
String[] strArr = str.split(",");
for (String dateStr : strArr) {
String replace = dateStr.replace("/", "-");
try {
simpleDateFormat.setLenient(false);
simpleDateFormat.parse(replace);
} catch (Exception e){
return false;
if(dateStr.matches(dateRegex)){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
try {
simpleDateFormat.setLenient(false);
simpleDateFormat.parse(dateStr);
} catch (Exception e){
return false;
}
} else {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String replace = dateStr.replace("/", "-");
try {
simpleDateFormat.setLenient(false);
simpleDateFormat.parse(replace);
} catch (Exception e){
return false;
}
}
}
}
return true;
}
//yyyy年MM月dd日,yyyy/MM/dd 转 yyyy-MM-dd
public static String coverDate(String str) throws ParseException {
List<String> newStrList = new ArrayList<>();
String dateRegex = "^\\d{4}年\\d{1,2}月\\d{1,2}日";
str = str.replace(",",",");
String[] strArr = str.split(",");
for (String dateStr : strArr) {
String newStr = null;
if (dateStr.matches(dateRegex)) {
Date date = new SimpleDateFormat("yyyy年MM月dd日").parse(dateStr);
newStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else if (dateStr.contains("/")) {
Date date = new SimpleDateFormat("yyyy/MM/dd").parse(dateStr);
newStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
} else if (dateStr.contains("-")){
newStr = dateStr;
}
newStrList.add(newStr);
}
return org.apache.commons.lang3.StringUtils.join(newStrList, ",");
}
}