我正在尝试计算日期之间的时间差异:
public static void main(String args[])
{
String start = "2013-03-10 10:28:47.000000";
String end = "2013-04-07 09:54:08.054577";
CalculateDuration calc = new CalculateDuration();
calc.calculateDiffrenceInMinutes(start,end);
}
public int calculateDiffrenceInMinutes(String start_time, String end_time)
{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(start_time);
d2 = format.parse(end_time);
} catch (ParseException e) {
e.printStackTrace();
}
// Get msec from each, and subtract.
int diff = (int) d2.getTime() - (int)d1.getTime();
int diffSeconds = (int) (diff / 1000);
int diffMinutes = (int) (diff / (60 * 1000));
int diffHours = (int) (diff / (60 * 60 * 1000));
return diffMinutes;
}
出于某种原因,即使结束日期在开始日期之后,我也无法理解我展示的示例给出负输出。
有任何想法吗?