English 中文(简体)
日间、小时、分钟和洋底二人的日期和时间差异
原标题:Get Date and Time Difference in Days, Hours, Minutes and Seconds in Android
  • 时间:2012-05-17 10:59:30
  •  标签:
  • android

我需要在目前的日期和将来的日期之间,在白天、小时、分钟和二秒之间有所区别。 如目前的日期和时间为17/05/201203:53:00,今后的日期为18/05/2012 04:5。

我需要显示剩下的时间:1小时:1小时和2分钟。

任何类型的帮助都将受到赞赏。 很多人预先表示感谢。

Regards, Munazza K

问题回答

你们可以这样做。

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

你可以减去日期和计算差异。 愿:

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;

你可以减去你已经计算出的时间。 你们需要时间、休息时间、发言时间等。

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/

//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;
        }

从所有这些答复来看,这是我所要到的。

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

希望:

rel=“nofollow”Date

With getTime() method, you can have the time in miliseconds of the 2 Dates. Then you can create another Date object 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.





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签