| | |
| | | import java.util.Locale; |
| | | |
| | | public class Vtil { |
| | | |
| | | public static boolean isValidDbDate(String dateStr) { |
| | | // 1. 空值/空字符串直接返回false |
| | | if (dateStr == null || dateStr.trim().isEmpty()) { |
| | | return false; |
| | | } |
| | | |
| | | // 2. 定义严格的日期格式化器(yyyy-MM-dd),禁止宽松解析 |
| | | DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") |
| | | .withResolverStyle(java.time.format.ResolverStyle.STRICT); |
| | | |
| | | try { |
| | | // 3. 尝试解析字符串为LocalDate |
| | | LocalDate.parse(dateStr, dateFormatter); |
| | | // 解析成功,说明格式和日期都合法 |
| | | return true; |
| | | } catch (DateTimeParseException e) { |
| | | // 解析失败(格式错误/日期非法,如2024-02-30),返回false |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | public static String defaultValue(Object o) { |
| | | return o == null ? "" : o.toString(); |
| | | // 1. 如果入参为null,直接返回空字符串 |
| | | if (o == null) { |
| | | return null; |
| | | } |
| | | |
| | | // 2. 将对象转为字符串并去除首尾空格 |
| | | String str = o.toString().trim(); |
| | | |
| | | // 3. 如果处理后是空字符串(原字符串只有空格),返回null;否则返回处理后的字符串 |
| | | return str.isEmpty() ? null : str; |
| | | } |
| | | |
| | | public static String defaultValue(Object o, String defaultValue) { |
| | | // 1. 如果入参为null,直接返回空字符串 |
| | | if (o == null) { |
| | | return defaultValue; |
| | | } |
| | | |
| | | // 2. 将对象转为字符串并去除首尾空格 |
| | | String str = o.toString().trim(); |
| | | |
| | | // 3. 如果处理后是空字符串(原字符串只有空格),返回null;否则返回处理后的字符串 |
| | | return str.isEmpty() ? defaultValue : str; |
| | | } |
| | | |
| | | public static String defaultValueToNumber(Object obj) { |
| | | // 1. 处理null值 |
| | | if (obj == null) { |
| | | return ""; |
| | | return null; |
| | | } |
| | | |
| | | // 2. 将对象转为字符串(兼容各种类型) |
| | |
| | | |
| | | public static Double defaultDoubleSplit(Object o) { |
| | | return o == null || o.equals("") ? null : Double.parseDouble(defaultValue(o).split("/")[0]); |
| | | } |
| | | |
| | | public static String defaultValue(Object o, String defaultValue) { |
| | | return o == null || o.equals("") ? defaultValue : o.toString(); |
| | | } |
| | | |
| | | public static String defaultValueToNull(Object o) { |