计算两个Java日期实例之间的差异
我在Scala中使用Java的 java.util.Date 类,并想比较 Date 对象和当前时间。 我知道我可以使用getTime()计算增量: (new java.util.Date()).getTime() - oldDate.getTime() 但是,这只给我留了 long 毫秒。 有没有更简单,更好的方法来获取时间增量? #1楼 简单差异(无lib) /** * Get a diff between two dates * @param date1 the oldest date * @param date2 the newest date * @param timeUnit the unit in which you want the diff * @return the diff value, in the provided unit */ public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) { long diffInMillies = date2.getTime() - date1.getTime(); return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS); } 然后您可以致电: getDateDiff