public class testDateCompare {
public static void main(String[] args) {
String date1="2011-01-29 16:12:20";
String date2="2011-01-29 16:12:25";
System.out.println(dateCompareForString(date1,date2));
}
/**
*
* @param date1 较小的时间
* @param date2 较大的时间
* @return
*/
private static String dateCompareForString(String date1, String date2){
String result = "";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date bigDate;
Date smalDate;
try {
bigDate = df.parse(date2);
smalDate=df.parse(date1);
long l=bigDate.getTime()-smalDate.getTime();
long day=l/(24*60*60*1000);
long hour=(l/(60*60*1000)-day*24);
long min=((l/(60*1000))-day*24*60-hour*60);
long s=(l/1000-day*24*60*60-hour*60*60-min*60);
result = min+"分"+s+"秒";
} catch (ParseException e) {
e.printStackTrace();
}
return result;
}
}