1、String使用ReplaceAll()后,把结果再赋给自己:
public static boolean inDateField(S2s bean1, Flag bean2) {
String formDate = bean1.getForm_date();
if (bean2 == null) {
return false;
}
String startDate = bean2.getStartDate();
String endDate = bean2.getEndDate();
if (formDate == null || "".equals(formDate) || startDate == null
|| "".equals(startDate) || endDate == null
|| "".equals(endDate)) {
return false;
}
startDate = startDate.replaceAll("-", "");
endDate = endDate.replaceAll("-", "");
return (formDate.compareTo(startDate) >= 0)
&& (formDate.compareTo(endDate) <= 0);
}
以前写的:
startDate.replaceAll("-", "");
endDate.replaceAll("-", "");
没有将结果赋给原来的String,所以原String没有去掉"-",比对结果自然是错的。
2、要注意正则表达式:
在使用时,"."、"/"等等属于正则表达式的特殊字符,要特别注意!
public static void testReplaceAll() {
String startDate = "2008.09.01";
String endDate = "2008.09.30";
String sDate = startDate.replaceAll("[.]", "");
String eDate = endDate.replaceAll("[.]", "");
System.out.println(startDate + ":" + endDate + ":" + sDate + ":"
+ eDate);
}