feat: 导入年份限制大于1970年

This commit is contained in:
2023-11-08 14:38:23 +08:00
parent ae07baaba1
commit 17057c704f
@@ -511,12 +511,24 @@ public class ImportUtil {
// 单选日期校验 // 单选日期校验
private boolean singleDateValid(String cellValue) { private boolean singleDateValid(String cellValue) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 设置lenient为false,使得日期解析更为严格 // 设置lenient为false,使得日期解析更为严格
sdf.setLenient(false); sdf.setLenient(false);
try { try {
// 使用parse方法而不是format方法 Date date = sdf.parse(cellValue.trim()); // 解析日期
sdf.parse(cellValue.trim()); // 这里还添加了trim()以去除可能的空白字符
return true; // 获取日期中的年份
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
// 检查年份是否大于1970
if (year > 1970) {
return true;
} else {
log.error("年份必须大于1970");
return false;
}
} catch (ParseException e) { } catch (ParseException e) {
// 这里捕获的应该是ParseException // 这里捕获的应该是ParseException
log.error("日期转换错误: {}", e.getMessage()); log.error("日期转换错误: {}", e.getMessage());
@@ -525,22 +537,33 @@ public class ImportUtil {
} }
private boolean multiDateValid(String cellValue) { private boolean multiDateValid(String cellValue) {
String dataPath = "yyyy-MM-dd"; String dateFormatPattern = "yyyy-MM-dd";
String[] dates = cellValue.split(","); String[] dates = cellValue.split(",");
SimpleDateFormat sdf = new SimpleDateFormat(dataPath); SimpleDateFormat sdf = new SimpleDateFormat(dateFormatPattern);
// 设置lenient为false,使得日期解析更为严格
sdf.setLenient(false); sdf.setLenient(false);
try { try {
for (String date : dates) { for (String dateStr : dates) {
if (date.length() < dataPath.length()) { if (dateStr.length() < dateFormatPattern.length()) {
// 如果日期字符串长度小于格式字符串长度,则直接返回false
log.error("日期格式长度错误");
return false;
}
// 解析日期字符串
Date date = sdf.parse(dateStr.trim());
// 使用Calendar获取年份
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
// 检查年份是否大于1970
if (year <= 1970) {
log.error("年份必须大于1970");
return false; return false;
} }
// 使用parse方法而不是format方法
sdf.parse(date.trim()); // 这里还添加了trim()以去除可能的空白字符
} }
// 所有日期验证通过
return true; return true;
} catch (ParseException e) { } catch (ParseException e) {
// 这里捕获的应该是ParseException
log.error("日期转换错误: {}", e.getMessage()); log.error("日期转换错误: {}", e.getMessage());
return false; return false;
} }