我会实现一个方法,在输入中接收两个对象日期并计算这两者之间传递的时间。我怎么能实现这样的东西?有些想法?
答案 0 :(得分:2)
您期望答案的单位是什么?这是一种可能性:
public static String timeBetween(Date start, Date end) {
long timeDiff = Math.abs(end.getTime() - start.getTime());
String diff = String.format("in days: %d | in hours: %d | in minutes: %d",
TimeUnit.MILLISECONDS.toDays(timeDiff),
TimeUnit.MILLISECONDS.toHours(timeDiff),
TimeUnit.MILLISECONDS.toMinutes(timeDiff));
return diff;
}
答案 1 :(得分:1)
public long getDifference(Date strt, Date end){
long s = strt.getTime();
long e = end.getTime();
return e-s;
}
返回它们之间的毫秒数
答案 2 :(得分:1)