处理日期

This commit is contained in:
zhou-hongcheng
2026-03-16 10:02:51 +08:00
parent 5998f1b056
commit a7674084e4
@@ -906,13 +906,30 @@ public class ShiftManageDomainService {
}
}
public Date stringToDate(String dateStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
throw new RuntimeException(e);
public Date stringToDate(String dateStr) {
if (dateStr == null || dateStr.trim().isEmpty()) {
return null;
}
// 去除首尾空格
dateStr = dateStr.trim();
// 定义可能的格式
String[] patterns = {
"yyyy-MM-dd HH:mm:ss", // 完整时间
"yyyy-MM-dd HH:mm", // 到分钟
"yyyy-MM-dd", // 只有日期 (报错的这个)
"yyyy/MM/dd HH:mm:ss",
"yyyy/MM/dd"
};
for (String pattern : patterns) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
// 设置严格解析,避免将 2026-03-0112:00:00 这种错误数据解析成功
sdf.setLenient(false);
return sdf.parse(dateStr);
} catch (ParseException e) {
}
}
throw new RuntimeException("无法解析的日期格式: " + dateStr + ",支持格式:yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss");
}
/** PO 转 DO,含 children 递归转换 */