countdown timer in android - android

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.

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

Related

How to set the value of count down timer on app start

Hey there i have a countdown timer which i used to show the user how much time is left in my app. But now i want if timer is running for 1 min and user closes the app after 30 sec and in next 5 sec user again open the app then time should start to run from remaining 25 seconds
CountDownAdapter countDown = new CountDownAdapter(60*1000, 1000);
countDown.setSourceActivity(MainActivity.this);
countDown.start();
public void onCountDownTimer(long millisUntilFinished)
{
secondsLefts = millisUntilFinished;
long secondsLeft = millisUntilFinished / 1000;
long hours = secondsLeft / 3600;
long minutes = (secondsLeft % 3600) / 60;
long seconds = secondsLeft % 60;
timerText = hours + "h: "+minutes + "m: "+seconds + "s";
if(timerText.equals("0h: 0m: 1s"))
{
}
timer.setText(timerText);
}
The lifecycle of an app looks like this:
So try to store the current remaining time and the exact current timestamp when onPause() is called (the user switches to another app). And when the user opens the app again, you need to catch the onResume() function.
Read out the stored timestamp and compare it with the current time. The time difference can now be substracted from the remaining time that you also stored.
There might be a better solution but it would be the easiest way to store the timestamp and remaining time in an XML file.

Android get the date difference in minutes

I am trying to get the difference of two dates and get the results in minutes. i have the two dates in milliseconds
long start = 1447143052593L;
long end = 1447146592540L;
output of above is
I/System.out﹕ 03:10:52
I/System.out﹕ 04:09:52
what i expect to be 0:59
And i tried to get the difference in the below way, it does not work.
long mills = end - start;
long Hours = mills/(1000 * 60 * 60);
long Mins = mills % (1000*60*60);
String diff= Hours + ":" + Mins;
And the when i print the String diff i get the result as below
I/System.out﹕ 0:3539947
try this approach:
long Hours = mills / (1000 * 60 * 60);
long Mins = (mills - Hours * (1000 * 60 * 60)) / (1000 * 60);
You are on the correct way, but your Mins now has the milliseconds that are remaining after the hours, instead of the minutes that are remaining.
This should work:
long Mins = (mills % (1000*60*60)) / (1000*60); //make it minutes
Usually you just need to make a Date object
Date startDate = new Date(start);
Date endDate = new Date(end);
int startminutes = startDate.getMinutes();
int endminutes = endDate.getMinutes();
And i say usually because it looks like it has been deprecated some time ago (while i hope it should be still working).
Looks like now they want we to use the Calendar class for this.
Hope this helps.

How to create a countdown till a specific day Android

I want to make a countdown until a certain day but I don't know how to do it.
I want the countdown to count days, hours, minutes and seconds.
The final day will be set into the countdown with the format DAY/MONTH/YEAR. Ex: 11/9/15
Thank you and sorry for my English :P
EDIT:
What I want is the next:
You have a string that's a date (20/9/15). I want to make a countdown that counts DAYS, HOURS, MINUTES & SECONDS from today till the date. The countdown should be displayed on a textView
Thanks :D
Assuming you're using Java (you don't say), use the getTime method of the java.util.Date objects indicating now and then, get the difference between them to figure out the number of days, hours, minutes, etc... remaining.
public String timeRemaining(Date then) {
Date now = new Date();
long diff = then.getTime() - now.getTime();
String remaining = "";
if (diff >= 86400000) {
long days = diff / 86400000
remining += "" + days + (days > 1) ? "days" : "day";
diff -= days * 86400000;
}
//... similar math for hours, minutes
return remaining;
}

Android: SimpleDateFormat keeps adding 1 hour to my time

I'm using currentTimeMillis(); to get a start time, then later using it again to get an end time. I then use delete start from end and get a value which is the duration between the two. I am using SimpleDateFormat to make all these values pretty and readable. the only thing is when i'm using a low value like 10 seconds (or 300 etc) and not the full blown long number (i.e. 1335718053126) I appear be getting out 01:00:10 or 01:02:12 etc on all my outputs? in fact if I just ask SimpleDateFormat to output a hh:mm:ss value against a 0 value it reads 01:00:00.
any one know why this is?
Found this neat little code if anyone else needs a solution to time formatting.
Source link
public String getNiceTime(long time) {
String format = String.format("%%0%dd", 2);
String seconds = String.format(format, time % 60);
String minutes = String.format(format, (time % 3600) / 60);
String hours = String.format(format, time / 3600);
String outPutTime = hours + ":" + minutes + ":" + seconds;
return outPutTime;
}
mySimpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Explanation: you are probably living in a GMT+1 time zone and unless you specify a different one, your formatter will pick your current one, so it considers 0 hours as GMT and as you are in GMT+1, it outputs 1 hour

Counting dates on Android

Well i'm currently building an app for android and i need to store a day and count how many days until that day comes.
I store the day on shared prefs. First i initialize the calendars.
Calendar next = Calendar.getInstance();
Calendar now = Calendar.getInstance();
Then i set the "next" calendar
nday = prefs.getInt("d", 0);
nmonth = prefs.getInt("m",0);
nyear = prefs.getInt("y",0);
next.set(nyear, nmonth, nday);
Then i do this to calculate how many days left.
diff =next.getTimeInMillis()-now.getTimeInMillis();
diffDays = diff / (24 * 60 * 60 * 1000);
output.setText(diffDays + " Days left");
And here is the problem. The calculator was working great until 2 days ago. When it supposed to say "3 days" it was writing "2 days" and it still goes one day wrong. If i try close and open the app, sometimes it calculates the days correct and sometimes it misses one day... Can someone understands whats wrong? I have diff and diffDays as long. I tried cast them as int but i still got the same problem, sometimes it writes 3 days left, sometimes 2....
ok i found out how to solve this. It seems that the getInstance have difference in milliseconds so i did this
Calendar now = Calendar.getInstance();
now.set(Calendar.HOUR_OF_DAY,00);
now.set(Calendar.MINUTE ,00);
now.set(Calendar.SECOND,00);
now.set(Calendar.MILLISECOND,00);
get the day from shared prefs
nday = extras.getInt("nDay");
nmonth = extras.getInt("nMonth");
nyear = extras.getInt("nYear");
//set the calendar
next.set(nyear, nmonth, nday, 00, 00,00);
next.set(Calendar.MILLISECOND,00);
and finally calculate the difference
long diff = 0;
diff = next.getTimeInMillis()-now.getTimeInMillis();
diffDays = diff / (24 * 60 * 60 * 1000);
output.setText(diffDays + " Days");
now i get the real difference without any mistakes, thanks everyone for your help!
Because it is implicitly casting to long, so you are losing some of your minutes and hours.
Declare diffDays as double, then you can show to the user when there are, for example 2.5 days and it won't show it as 2 days. Or, you could take the integer part for the days, and calculate the hour from the fraction:
int hour = (diffDays - Math.floor(diffDays))*24
This gives you the minute. Just make sure you did declare diffDays as double or float.
Also use the calendar for calculating dates.
Calendar diff = Calendar.getInstance();
long diffMillis = next.getTimeInMillis()-now.getTimeInMillis();
diff.setTimeInMilliSeconds(Math.abs(diffMillis));
int days = diff.get(Calendar.DAY_OF_YEAR);
if (diffMillis < 0L) {
days *= -1;
}

Categories

Resources