解决思路
一般常见的思路有两种:利用Calendar 和 利用Date;
利用 java.util.Calendar
使用的大致方法如下:
但是 你会发现实际使用的时候会出现问题,因为使用的是减法,会出现 负数,也就是说意味着需要向前借位。
不过 做也是能做,但是需要从秒 -> 年 ,从小到大,然后记录是否需要借位,如代码块二,但也是较为复杂
所以 不推荐使用吧
Calendar c1 = Calendar.getInstance();
c1.setTime(startTime);
Calendar c2 = Calendar.getInstance();
c2.setTime(endTime);
int yearDiff = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
if (yearDiff != 0) {
durationStr += yearDiff + "年";
}
int mouthDiff = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
if (mouthDiff != 0) {
durationStr += mouthDiff + "月";
}
int dayDiff = c2.get(Calendar.DAY_OF_MONTH) - c1.get(Calendar.DAY_OF_MONTH);
if (dayDiff != 0) {
durationStr += dayDiff &#