5

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

Munazza
  • 361
  • 2
  • 6
  • 15
  • You may also find this post useful, as it describes correct way to calculate difference between two dates: https://stackoverflow.com/a/6406294/716075 – StenaviN May 17 '12 at 11:24

5 Answers5

18

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;`
Khan
  • 7,585
  • 3
  • 27
  • 44
8

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.

RMartins
  • 171
  • 1
  • 1
  • 6
  • This just gives the length of the entire time duration in different units, not a breakdown of the days, hours, and minutes remaining as the OP implies. – Martin Erlic May 23 '17 at 12:45
1

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;
        }
Damian
  • 8,062
  • 4
  • 42
  • 43
1

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

Stephen Vinouze
  • 1,815
  • 1
  • 17
  • 28
0

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.

gutiory
  • 1,135
  • 5
  • 13
  • 33