日期:2014-05-18 浏览次数:20838 次
package org.vicky.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.Map;
public class TestComparator implements Comparator<Map<String, String>> {
@Override
public int compare(Map<String, String> o1, Map<String, String> o2) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = null;
Date d2 = null;
for (String k : o1.keySet()) {
try {
d1 = format.parse(o1.get(k).substring(11));
} catch (ParseException e) {
e.printStackTrace();
}
}
for (String k : o2.keySet()) {
try {
d2 = format.parse(o2.get(k).substring(11));
} catch (ParseException e) {
e.printStackTrace();
}
}
if (d1.before(d2)) {
return 1;
} else if (d1.after(d2)) {
return -1;
} else {
return 0;
}
}
}
package org.vicky.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map1 = new HashMap<String, String>();
map1.put("FINISH_DATA=2013-03-09", "BEGIN_DATA=2013-03-03");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("FINISH_DATA=2013-01-19", "BEGIN_DATA=2013-01-13");
Map<String, String> map3 = new HashMap<String, String>();
map3.put("FINISH_DATA=2013-01-12", "BEGIN_DATA=2013-01-06");
Map<String, String> map4 = new HashMap<String, String>();
map4.put("FINISH_DATA=2013-01-05", "BEGIN_DATA=2012-12-30");
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
Collections.sort(list, new TestComparator());
for (Map<String, String> m : list) {
for (Map.Entry<String, String> en :