代码:
/**
* 比较两个时间的大小
* @param source 当前时间
* @param traget 需要比较的时间
* @param type 日期格式
* @return
*/
public static boolean dateCompare(String source, String traget, String type) {
boolean b = false;
try {
SimpleDateFormat format = new SimpleDateFormat(type);
Date sourcedate = format.parse(source);
Date tragetdate = format.parse(traget);
int ret = sourcedate.compareTo(tragetdate);
switch (ret) {
case 0:
System.out.println("两个时间相等");
b = true;
break;
case 1:
System.out.println("当前时间晚于" + traget);
b = true;
break;
case -1:
System.out.println("当前时间早于" + traget);
break;
default:
break;
}
} catch (Exception e) {
e.getMessage();
}
return b;
}