I know here is lot of question on "start/stop timer".But i am looking for format of
timer. I am developing one recording application which show record time.
I able to show time in second format like 1 2 3 4 5. but i need to show this time like
00.01. I need some hint or reference. here is image which i need to show.
Thanks in Advance
Here is the very nice tutorial of simple countdown timer. Go through it and you will be able to achieve what you want.
Or A Stitch in Time is the efficient way to implement the stop watch type app from developer.android.com and yes it uses the format you required.
You can try this:
private String stringForTime(int timeMs) {
StringBuilder mFormatBuilder = new StringBuilder();
Formatter mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
int totalSeconds = timeMs / 1000;
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
mFormatBuilder.setLength(0);
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
Related
I have a mp3 file and my application must seek to some selected time of that mp3 file then start playing from there.
I convert my string time by this method to int value
private static int convert(String time) {
int quoteInd = time.indexOf(":");
int pointInd = time.indexOf(".");
int min = Integer.valueOf(time.substring(0, quoteInd));
int sec = Integer.valueOf(time.substring(++quoteInd, pointInd));
int mil = Integer.valueOf(time.substring(++pointInd, time.length()));
return (((min * 60) + sec) * 1000) + mil;
}
Note: my stings is like this 5:12.201 that means 5 mins and 12 seconds and 201 milliseconds.
I getted these times from MP3 Audio Editor application (it's for windows) and I checked theme with KMPlayer application (it's for windows). And these times was correct on both of them.
But in my app when I seek my MediaPlayer to that time, audio doesn't start from my selected position. (time is correct but sound is different.)
I thought that the MediaPlayer doesn't seek to that time correctly. So I checked current position by calling getCurrentPosition() before playing but returned value and seeked value was same.
I haven't any Idea about It.
Edit:
My problem is NOT time converting.
I convert and seek to there correctly but it play something that not expected in that time.
It means timing in KMPlayer and Android are differents.
My question is Way? and How can is solve it?
You must seek using the seekTo method. This expects miliseconds as time offset. Your offset is how far from the beginning you want to play, for example 1 minute from start can be your offset.
Note: my strings is like this 5:12.201 (Mins : Secs : Millisecs)
If you want to seek to a time like 5:12.201 then use seekTo(312201);
Explained :
1000 ms gives you one second, so 12000 is 12 seconds, and 60000 is one minute.
If you want 5m:12s time then do as :
MyMins = 1000 * 60 * 5; //# 5 mins at 60 secs per minute
MySecs = 1000 * 12; //# 12 secs
MyMilliSecs = 201; //# 201 millisecs
SeekValue = (MyMins + MySecs + MyMilliSecs);
seekTo(SeekValue); //# seeks to 312201 millisecs (is == 5 min & 12 secs & 201 ms)
I usually use this function.
I think It can help to you.
public static String milliSecondsToTimer(long milliseconds){
String finalTimerString = "";
String secondsString = "";
int hours = (int)( milliseconds / (1000*60*60));
int minutes = (int)(milliseconds % (1000*60*60)) / (1000*60);
int seconds = (int) ((milliseconds % (1000*60*60)) % (1000*60) / 1000);
if(hours > 0){
finalTimerString = hours + ":";
}
if(seconds < 10){
secondsString = "0" + seconds;
}else{
secondsString = "" + seconds;}
finalTimerString = finalTimerString + minutes + ":" + secondsString;
return finalTimerString;
}
Good luck :)
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.
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;
}
Im trying to get the time elapsed from a specific date (in millis)
In some devices I get the correct result
however, in other devices I get wrong result
here's the code that i use:
public static void timeElapsedFromDate(long workingDate)
{
long diff = System.currentTimeMillis() - workingDate;
long diffSeconds = diff / 1000;
long diffMinutes = diffSeconds / 60;
long diffHours = diffMinutes / 60;
long diffDays = diffHours / 24;
long diffWeeks = diffDays / 7;
long diffMonth = diffDays / 30;
}
Isn't it the way to measure time?
thanks!
First of all, diffMonth formula asks to be improved: from Feb 1st to Mar 1st it is usually 28 days, but one month. From Sep 1st to Sep 31st it is 30 days, but less than 1 month.
More subtle question regarding diffDays: isn't it 1 day between 10 PM and 10 AM next morning?
Finally, how different devices can give different results: System.currenTTimeMillis() returns the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. Even if the clock on all devices is correct, they may have different Time Zone settings.
public int getElapsedTimeInDays(Date start,Date end){
int days=(int)(start.getTime()-end.getTime())/(1000*60*60*24);
return days;
}
public int getElapsedTimeInHours(Date start,Date end){
int hours=(int)(start.getTime()-end.getTime())/(1000*60*60);
return hours;
}
public int getElapsedTimeInMinutes(Date start,Date end){
int minutes=(int)(start.getTime()-end.getTime())/(1000*60);
return minutes;
}
public int getElapsedTimeInSecunds(Date start,Date end){
int scunds=(int)(start.getTime()-end.getTime())/(1000);
return secunds;
}
//Then you can use the codes like this:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd, hh:mm");
Date start = df.parse("2012-01-12, 09:35");
Date end = new Date();//this will be the curent date;
int day = getElapsedTimeInDays(start,end);
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;
}