一、判断字符串是否为数字(包括金额符号、整数、小数)。
备注:“$12,345.56” 或 “$ 12,345.56” 或 “¥12,345.56” 或 “¥345”,返回的结果都是true。
public static boolean isNumeric(String numStr) {
if (numStr == null) {
return false;
}
try {
Double.parseDouble(numStr.replaceAll("[$¥,,\\s]", ""));
return true;
} catch (NumberFormatException e) {
return false;
}
}
二、判断字符串是否为日期。
备注:“yyyy-MM-dd HH:mm:ss” 或 “yyyy-MM-dd HH:mm” 或 “yyyy-MM-dd” 或 “yyyy/MM/dd HH:mm:ss” 或 “yyyy/MM/dd HH:mm” 或 “yyyy/MM/dd”类型的日期,返回的结果都是true。
public static boolean isDate(String dateStr) {
if (dateStr == null) {
return false;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
sdf.parse(dateStr.trim().replaceAll("/", "-"));
return true;
} catch (ParseException e) {
return false;
}
}