求比较日期的算法
有这样一条表达式, 类型当它是日期, 除了A 不为空外, BCDEFG 都为可以空.
A < B < C <= D <= E < F <= G
(1) 判断这些数是否为空, 如果不为空, 就按上面的规律进行对比. 如果等式例如 B <C
不成立时, 报错记号并继续比较,对比完再把不合格的报出来.
(2) 当B为空时, C往上找比较对象 , 找A比较, 如 A <C ,如此类推. B为空, C为空时, D就去找A比较一路比下去.
请教一下有没有好的算法?
[解决办法]
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @class :.CompareDate.java
* @author :xufei
* @createDate :Aug 5, 2008 9:49:14 AM
*/
public class CompareDate {
String a = "20070101";
String b = "20080101";
String c = "20070201";
String d = "";
String e = "20070301";
String f = "";
String g = "20070404";
List<String> errorList = new ArrayList<String>();
public void compare(){
//先把空的去掉
List<String> list = getNoNullList();
int count = list.size();
for (int i = 0; i < count - 1; i++) {
String firstDate = list.get(i);
String secondDate = list.get(i + 1);
if(firstDate.equals("c")
[解决办法]
firstDate.equals("d")
[解决办法]
firstDate.equals("f")) {
if(firstDate.compareTo(secondDate) > 0) {
errorList.add(firstDate);
}
} else {
if(firstDate.compareTo(secondDate) >= 0) {
errorList.add(firstDate);
}
}
}
System.out.print(errorList.toString());
}
private List getNoNullList() {
List list = new ArrayList();
addList(list, a);
addList(list, b);
addList(list, c);
addList(list, d);
addList(list, e);
addList(list, f);
addList(list, g);
return list;
}
private void addList(List list, String src) {
if(!src.equals("")){
list.add(src);
} else {
errorList.add(src);
}
}
public static void main(String a []) {
CompareDate compareDate = new CompareDate();
compareDate.compare();
}
}
[, , 20080101]