Munazza
Munazza

Reputation: 361

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

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.

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

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

Regards, Munazza K

Upvotes: 5

Views: 24010

Answers (5)

Stephen Vinouze
Stephen Vinouze

Reputation: 2065

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

Upvotes: 1

Damian
Damian

Reputation: 8072

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

Upvotes: 1

Khan
Khan

Reputation: 7605

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

Upvotes: 18

gutiory
gutiory

Reputation: 1135

Use Date class.

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.

Upvotes: 0

RMartins
RMartins

Reputation: 161

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.

Upvotes: 8

Related Questions