- Java中比较日期:
ComparatorDate.java类
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import bean.Meet;
public class ComparatorDate implements Comparator{
@Override
public int compare(Object obj1, Object obj2) {
Meet t1 = (Meet) obj1;
Meet t2 = (Meet) obj2;
Date d1, d2;
d1 = t1.getStart();
d2 = t2.getStart();
if (d1.before(d2)) { //时间小的优先级低,故排在前;也就是优先级高的排在后面
return -1;
} else {
return 1;
}
}
}
调用:
//将符合条件的会议时间排序
ComparatorDate c = new ComparatorDate();
Collections.sort(meets, c);
- java计算运行时间
//计算不同特征序列的排序耗时
for(int i=1; i<=10; i++){
a = new Random().ints(i*1000).toArray(); //随机生成一定规模的数组
//随机序列排序所需时间
long begin1 = System.currentTimeMillis();
sort(a);
long end1 = System.currentTimeMillis();
//正序序列排序所需时间
long begin2 = System.currentTimeMillis();
sort(a);
long end2 = System.currentTimeMillis();
Integer[] arr = new Integer[a.length];
for(int k=0; k<a.length; k++){
arr[k] = new Integer(a[k]);
}
Arrays.sort(arr, Collections.reverseOrder());
for(int k=0; k<a.length; k++){
a[k] = arr[k];
}
//逆序排列排序所需时间
long begin3 = System.currentTimeMillis();
sort(a);
long end3 = System.currentTimeMillis();
System.out.print("随机序列: " + (end1 - begin1) + "毫秒");
System.out.print("; 正序序列: " + (end2 - begin2) + "毫秒");
System.out.println("; 逆序序列: " + (end3 - begin3) + "毫秒");
}
网友评论