2

i want to run an countdown time , in which i want to show days,hours,sec and milisec remaining for a specific date. and will be be keep changing till the end of the specific date. Hope you can understand.

Thanks in advance.

Sujit
  • 10,512
  • 9
  • 40
  • 45
  • 2
    And what exactly is your question? How to make the timer functionality, how to make the UI change every millisecond? Or do you just want us to paste all the code you need? – Janusz Sep 20 '10 at 07:58

1 Answers1

4

Well, I think the problem is, that you dont know, how to work with the time. Here i have a method I use to calculate the amount of time of some items which I parse out of a db. The param is a double value, which has got the whole time in seconds. It returns a string with the time in days, hours, minutes and seconds as string.

public String timeCalculate(double ttime) {

    long days, hours, minutes, seconds;
    String daysT = "", restT = "";

    days = (Math.round(ttime) / 86400);
    hours = (Math.round(ttime) / 3600) - (days * 24);
    minutes = (Math.round(ttime) / 60) - (days * 1440) - (hours * 60);
    seconds = Math.round(ttime) % 60;

    if(days==1) daysT = String.format("%d day ", days);
    if(days>1) daysT = String.format("%d days ", days);

    restT = String.format("%02d:%02d:%02d", hours, minutes, seconds);

    return daysT + restT;
}

For the countdown itself...take the target timestamp minus the actual one and voila, you've got seconds left :) Put those seconds to this method and you've got the remaining time. Now you just need to do some UI things ;) Oh, and for the usual Unix Timestamp you can use this little method:

public static long getTimestamp() {
    return System.currentTimeMillis() / 1000;
}
Keenora Fluffball
  • 1,647
  • 2
  • 18
  • 34
  • @keenora Fluffball I have used the above code and got the hours days minutes and secs but when i set the textview in onTick() of countdowntimer countdown doesnt start.Please See this for more info http://stackoverflow.com/questions/13833850/countdown-issue-while-displaying-days-hours-minutes-seconds-using-count-down-tim/13834196#comment19041515_13834196 – hemanth kumar Dec 12 '12 at 13:42
  • @Sujit I have used the above code and got the hours days minutes and secs but when i set the textview in onTick() of countdowntimer countdown doesnt start.Please See this for more info http://stackoverflow.com/questions/13833850/countdown-issue-while-displaying-days-hours-minutes-seconds-using-count-down-tim/13834196#comment19041515_13834196 – hemanth kumar Dec 12 '12 at 13:43