【问题标题】:Get Date and Time Difference in Days, Hours, Minutes and Seconds in Android在 Android 中获取日期和时间差(以天、小时、分钟和秒为单位)
【发布时间】:2012-05-24 22:52:30
【问题描述】:

我需要获取当前日期和未来日期之间的差异,在 android 中以天、小时、分钟和秒为单位。就像当前日期和时间是 17/05/2012 03:53:00 而未来的日期是 18/05/2012 04:55:00。

我需要将差异显示为剩余时间=天:1,小时:1,分钟 2。

我们将不胜感激任何形式的帮助。非常感谢。

问候, 穆纳扎K

【问题讨论】:

标签: android


【解决方案1】:

您可以减去两个日期,然后计算差异。有点像这样:

long difference = calendar.getTimeInMillis()-currentTime;

    long x = difference / 1000;
    seconds = x % 60;
    x /= 60;
    minutes = x % 60;
    x /= 60;
    hours = x % 24;
    x /= 24;
    days = x;

您可以减去已计算的时间。你得到时间,得到休息,做分钟,等等。

【讨论】:

  • 这只是给出了不同单位的整个持续时间的长度,而不是 OP 暗示的剩余天数、小时数和分钟数的细分。
【解决方案2】:

使用Date 类。

使用getTime() 方法,您可以获得两个日期的毫秒数。 然后,您可以创建另一个 Date 对象,其中包含两个日期之间减法的毫秒数结果。 您将获得自第一个 Date 以来的天数、小时数和分钟数。

【讨论】:

    【解决方案3】:

    你可以这样做

       c_date=18/05/2012 04:55:00.  and  saved_date=17/05/2012 03:53:00
    
       long diffInMillisec = c_date.getTime() -saved_date.getTime();
       long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMillisec);
       seconds = diffInSec % 60;
       diffInSec/= 60;
       minutes =diffInSec % 60;
       diffInSec /= 60;
       hours = diffInSec % 24;
       diffInSec /= 24;
       days = diffInSec;`
    

    【讨论】:

      【解决方案4】:

      我最近实现了同样的事情,我使用了 JodaTime。您可以从此处下载 jar 并将其包含在您的 Android 应用程序中:http://joda-time.sourceforge.net/

      //Create your taget date    
      Calendar cal = Calendar.getInstance();
          cal.clear();
          cal.set(Calendar.YEAR, 2012);
          cal.set(Calendar.MONTH, Calendar.JULY);
          cal.set(Calendar.DATE, 15);
          cal.set(Calendar.HOUR_OF_DAY, 8);
          Date startDate = cal.getTime();
      
      //Use JodaTime to calculate difference
          Period period =  getTimePassedSince(startDate);
      
      //Extract values and display
          daysTV.setText("" + Math.abs(period.getDays()));
          hoursTV.setText("" + Math.abs(period.getHours()));
          minsTV.setText("" + Math.abs(period.getMinutes()));
          secsTV.setText("" + Math.abs(period.getSeconds()));
      
          ...
          public static Period getTimePassedSince(Date initialTimestamp){
                  DateTime initDT = new DateTime(initialTimestamp.getTime());
                  DateTime now = new DateTime();
                  Period p = new Period(initDT, now, PeriodType.dayTime()).normalizedStandard( PeriodType.dayTime());
                  return p;
              }
      

      【讨论】:

        【解决方案5】:

        从所有这些回复中,这就是我想出的

        fun hoursBetween(date: Date): Long = TimeUnit.MILLISECONDS.toHours(date.time - Date().time)
        fun minutesBetween(date: Date): Long = TimeUnit.MILLISECONDS.toMinutes(date.time - Date().time) % 60
        fun secondsBetween(date: Date): Long = TimeUnit.MILLISECONDS.toSeconds(date.time - Date().time) % 60
        

        希望对你有帮助;)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-07
          • 2013-05-14
          • 1970-01-01
          相关资源
          最近更新 更多