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;
}
Related
I have a method which takes two String parameters. the two strings are Time values in 24 hour format. The Times are picked using a TimePicker from UI.
The goal of the method is to get the duration between the StartTime and EndTime in Minutes.
public static String getTimeDuration(String StartTime24, String EndTime24)
{
String duration = "";
try
{
SimpleDateFormat parseFormat = new SimpleDateFormat("HH:mm");
Date startTime = parseFormat.parse(StartTime24);
Date endTime = parseFormat.parse(EndTime24);
long mills = endTime.getTime() - startTime.getTime();
long minutes = mills/(1000 * 60);
duration = "(" + minutes + " Minutes)";
}
catch(ParseException ex)
{
// exception handling here
}
return duration;
}
The method works fine if both the times are within a Single Date. For example:
StartTime = 22:15
EndTime = 23:51
Output = (96 Minutes)
But my problem is, the method returns negative if the end time is after 12'o clock at night. For example,
StartTime = 23:51
EndTime = 0:55
Output = (-1376 Minutes)
What I want: (64 Minutes)
How can get the duration correct ?
As there is no date used, you have to check first if your endTime is less than your startTime. If yes, then your endTime is on the next day and you have to add 1 day/86 400 000 milliseconds. Then you will have your desired result.
Just add this condition:
if(endTime.getTime() < startTime.getTime()){
long mills = ((endTime.getTime() + 86400000) - startTime.getTime()); 1 day = 86 400 000 mill
}
Hope this helps
You're only parsing the minutes and hours. There's no day on there. So that puts both times on the same day (the first day of the epoch, Jan 1 1970 to be exact). So the answer is correct. If you want it to treat the end time as the next day if its earlier than the start time, then add 1 day to the result (1440 minutes) if the result is less than 0.
Essentially what I have is a string which contains a files Last Modified Date. To get this I'm using:
Date lastModDate = new Date(file.lastModified());
SimpleDateFormat formatter = new SimpleDateFormat("K:mm a");
String formattedDateString = formatter.format(lastModDate);
The end result is somewhat like 6:12 AM. What I want to do is each time a certain period of time is passed, the dateformat must change. E.g.
After 1 Day has gone by, Last Modified Date = ("Format1");
After a Week has gone by, Last Modified Date = ("Format2");
After 2 Weeks have gone by, Last Modified Date = ("Format3");
Does it make sense? If so is someone please be able to show me how it's done. A good example is the native Messaging App. When a message is created, It will show it's Time then after some days gone by the format changes to the Date it was created then the month etc...
I'm trying to do exactly that.
Calculate the difference in time between the last modified date and now:
long duration = lastModDate.getTimeInMillis() - current.getTimeInMillis();
long sec = TimeUnit.MILLISECONDS.toSeconds(duration);
boolean inFuture = sec > 0;
// Use positive value
if(!inFuture)
sec = -sec;
long minutes = sec / 60 % 60;
long hours = sec / 3600 % 24;
long days = sec / 86400;
if(days > 1 && days < 7)
// Use format 1
else if(days >= 7 && days < 14)
// Use format 2
else
// Use format 3
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
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;
}
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;
}