如果你只是想获得日期之间的日子里,你可以使用类似:
public static void main(String[] args) {
long initial = getTime("20-jul-2015 11:09:25"); /*you use System.currentTimeMillis() at the beginning*/
long finalTime = getTime("21-jul-2016 15:21:26"); /*you use System.currentTimeMillis() at the capture of final time.*/
printElapsedTime(initial, finalTime);
}
private static void printElapsedTime(long initial, long finalTime) {
long lapse = finalTime - initial;
long secs = (lapse/(1000))%60;
long mins = lapse/(1000*60)%60;
long hrs = lapse/(1000*60*60)%24;
long days = lapse/(1000*60*60*24);
StringBuilder lapseMsg = new StringBuilder("Elapsed time since ").append(new Date(initial)).append(" to " + new Date(finalTime)).append(":\r\n");
lapseMsg.append(days).append(" Days, ").append(hrs).append(" Hours, ").append(mins).append(" Minutes, ").append(secs).append(" seconds");
System.out.println(lapseMsg.toString());
}
/*just used to get any date to test.*/
private static long getTime(String date) {
DateFormat format = DateFormat.getDateTimeInstance();
try {
return format.parse(date).getTime();
} catch (ParseException e) {
throw new RuntimeException();
}
}
,如果你需要的东西更像是一个你可以使用日历逝去的几个月,做一些修正,以复杂的@ OldCurmudgeon解决方案。 (它并不适用于我的方式)
更容易与时期(可在Java 1.8) - [链接](https://docs.oracle.com/javase/8/docs/api/java/ time/Period.html) –
我们是24小时直线对话吗?或者您是否需要考虑由[夏令时](http://stackoverflow.com/tags/dst/info)转换创建的缺失时间间隔和重复时间重叠? –