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) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 设置lenient为false,使得日期解析更为严格
sdf.setLenient(false);
try {
// 使用parse方法而不是format方法
sdf.parse(cellValue.trim()); // 这里还添加了trim()以去除可能的空白字符
return true;
Date date = sdf.parse(cellValue.trim()); // 解析日期
// 获取日期中的年份
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) {
// 这里捕获的应该是ParseException
log.error("日期转换错误: {}", e.getMessage());
@@ -525,22 +537,33 @@ public class ImportUtil {
}
private boolean multiDateValid(String cellValue) {
String dataPath = "yyyy-MM-dd";
String dateFormatPattern = "yyyy-MM-dd";
String[] dates = cellValue.split(",");
SimpleDateFormat sdf = new SimpleDateFormat(dataPath);
// 设置lenient为false,使得日期解析更为严格
SimpleDateFormat sdf = new SimpleDateFormat(dateFormatPattern);
sdf.setLenient(false);
try {
for (String date : dates) {
if (date.length() < dataPath.length()) {
for (String dateStr : dates) {
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;
}
// 使用parse方法而不是format方法
sdf.parse(date.trim()); // 这里还添加了trim()以去除可能的空白字符
}
// 所有日期验证通过
return true;
} catch (ParseException e) {
// 这里捕获的应该是ParseException
log.error("日期转换错误: {}", e.getMessage());
return false;
}