在 Android 中获取以天、小时、分钟和秒为单位的日期和时差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10634420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:27:14  来源:igfitidea点击:

Get Date and Time Difference in Days, Hours, Minutes and Seconds in Android

android

提问by Munazza

I need to get difference between current date and a date in future, in days , hours , minutes and seconds in android. Like if current date and time is 17/05/2012 03:53:00 and a date in future is 18/05/2012 04:55:00.

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

I need to display difference as remaining time= day: 1, hours: 1, and minutes 2.

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

Any kind of help will be appreciated . Many thanks in advance.

任何形式的帮助将不胜感激。提前谢谢了。

Regards, Munazza K

问候, 穆纳扎 K

回答by Khan

You can do this way

你可以这样做

   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;`

回答by RMartins

You can subtract both dates, and the calculate the differences. Kinda like this:

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

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;

You can subtract the time you've already calculated. You get the hours, get the rest, do the minutes, etc.

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

回答by StenaviN

You may also find this post useful, as it describes correct way to calculate difference between two dates: https://stackoverflow.com/a/6406294/716075

您可能还会发现这篇文章很有用,因为它描述了计算两个日期之间差异的正确方法:https: //stackoverflow.com/a/6406294/716075

回答by Damian

I implemented the same thing recently, I used JodaTime. You can download the jar and include it in your Android app from here: http://joda-time.sourceforge.net/

我最近实现了同样的事情,我使用了 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;
        }

回答by ptitvinou

From all those responses this is what I came up to

从所有这些回应中,这就是我想到的

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

Hope it helps ;)

希望能帮助到你 ;)

回答by gutiory

Use Dateclass.

使用Date类。

With getTime()method, you can have the time in miliseconds of the 2 Dates. Then you can create another Dateobject with the amount of miliseconds results of the subtraction between both Dates. And you'll have the amount of days, hours and minutes since the first Date.

使用getTime()方法,您可以获得 2 个日期的时间(以毫秒为单位)。然后,您可以Date使用两个日期之间相减的毫秒数结果创建另一个对象。并且您将拥有自第一个以来的天数、小时数和分钟数Date