0

I got this. But i can't seem to work out how to get the number of days/hours/minutes/seconds left. I ran it in debug mode and it produced: 2776799998. 2776799998 in milliseconds is way to much for 2 days, 1 hour and 15 minutes. (Was when posting this).

What is the correct way?

Calendar cal = Calendar.getInstance();
cal.set(2012, 6, 28, 16, 0);
long endTime = cal.getTimeInMillis(); 
long currentTime = System.currentTimeMillis();
long remaining = endTime - currentTime;

long seconds = remaining / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
Patrick
  • 5,442
  • 9
  • 53
  • 104
  • It is about 32 days. Your month is 1 ahead - try using 5. – gkiar Jun 26 '12 at 12:58
  • What is your minSDKVersion? Use TimeUnit if possible. Your seconds calculation should use mod instead of division. See http://stackoverflow.com/questions/625433/how-to-convert-milliseconds-to-x-mins-x-seconds-in-java – nahwarang Jun 26 '12 at 13:04

2 Answers2

5

From Calendar's javadoc:

Month value is 0-based. e.g., 0 for January.

Piotr Praszmo
  • 17,928
  • 1
  • 57
  • 65
4

The month on the Calendar API starts in 0. So 6 corresponds to July. Maybe that's your problem.

Nuno Gonçalves
  • 6,202
  • 7
  • 46
  • 66