3

我想从 Date1 中得到多少日期、小时、分钟和秒数

String RemainingOn = "2013/25/1 22:36:24";
String mDate,mHour,mMinute,mSecond;

public String  DateComparedateTime()
{
 // compare  RemainingOn with now 
   mDate = Difference date with RemainingOn and Now;
   mHour = Difference hour with RemainingOn and Now;
   mMinute = Difference minute with RemainingOn and Now;
   mSecond = Difference second with RemainingOn and Now;

   return mDate+","+mHour+","+mMinute+","+mSecond ;
}
4

1 回答 1

2

以下代码给出了想法

public String getDateDiffString(Date dateOne, Date dateTwo)
{
    long timeOne = dateOne.getTime();
    long timeTwo = dateTwo.getTime();
    long oneDay = 1000 * 60 * 60 * 24;
    long delta = (timeTwo - timeOne) / oneDay;

    if (delta > 0) {
        return "dateTwo is " + delta + " days after dateOne";
    }
    else {
        delta *= -1;
        return "dateTwo is " + delta + " days before dateOne";
    }
}

如果你可以使用Joda Time

Joda Time 有一个时间间隔的概念:

Interval interval = new Interval(oldTime, new Instant());
于 2013-01-16T06:18:12.727 回答