Java两个日期相减
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDifferentExample {
public static void main(String[] args) {
String dateStart = "2013-02-19 09:29:58";
String dateStop = "2013-02-20 11:31:48";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
//毫秒ms
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
//指定小数点后面的位数(2位)
double result = (d1 / d2) * 100;
BigDecimal bd = new BigDecimal(result);
double cycleRate = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.print("两个时间相差:");
System.out.print(diffDays + " 天, ");
System.out.print(diffHours + " 小时, ");
System.out.print(diffMinutes + " 分钟, ");
System.out.print(diffSeconds + " 秒.");
System.out.print(cycleRate +"小数点后两位 秒.");
} catch (Exception e) {
e.printStackTrace();
}
}
}