tags:

views:

73

answers:

1

Hi, 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.

A: 

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
Thanks a lot Keenora....
Sujit