Android countdown clock

I need to show a countdown, I calculated the difference between dates in milliseconds, how can I convert this to a countdown, i.e. "20 days 12 hours 12 minutes left"? I can get the remaining hours.

(mili / 24 * 60 * 60 * 1000)

But this is the total number of hours remaining, how can I convert this to 0-24 hrs 0-60 min format?

+4
source share
2 answers
  //setting time Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal2.set(Calendar.DATE, 31); cal2.set(Calendar.HOUR_OF_DAY, 1); cal2.set(Calendar.MINUTE, 50); //printing dates System.out.println("Date1"+ cal1.getTime()); System.out.println("Date1"+ cal2.getTime()); long diffInMillis = cal2.getTimeInMillis() - cal1.getTimeInMillis(); System.out.println("Diff In Millis : " + diffInMillis); int hour = (int)(diffInMillis/(60.0 * 1000 * 60 )); int min = (int)((diffInMillis - (hour *60.0 *1000 *60))/(60.0*1000)); System.out.println("Diff In HH:MM: " + hour + ":" + min ); 
+3
source

You can use the standard java.util.Calendar class.

 Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(yourTime); int hour = cal.get(Calendar.HOURS_IN_DAYS); 
0
source

Source: https://habr.com/ru/post/1334038/


All Articles