-6

我有两个结果集,每个结果集只有一列。两个结果集的列字段都包含格式如下的时间条目(2012-12-31 13:49:21.999)。现在谁能帮我找出结果集两列之间的时间差?例如,如果第一个结果集的列的第(2013-02-13 17:04:09.672)一个字段有条目,而第二个结果集的列的第一个字段有条目,(2012-12-31 13:49:21.999)那么程序应该能够区分这两个输入的时间。

需要帮忙?

4

1 回答 1

2

就像是:

Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.ENGLISH).parse(column1);
Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.ENGLISH).parse(column2);
getDateDiff(date1,date2,TimeUnit.MINUTES);

/**
 * 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);
}
于 2013-09-13T08:39:52.480 回答