What do I have to change so that the timer is showing 10:00 after 10 minutes instead of 010:00. I know a bit how to solve it but it doesn't solve the original problem -- if delete the "0" from (textTimer.setText("0" + minutes + ":") it shows 0:00 when its under 10. How do I make it to show 10:00 after 10 minutes and 00:00 before 10 mins. Thanks
public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
textTimer.setText("0" + minutes + ":"
+ String.format("%02d", seconds));
myHandler.postDelayed(this, 0);
}
I'm not entirely sure why you're not formatting the entire string
textTimer.setText(String.format("%02d:%02d", minutes, seconds));
Related
I have a music player which has a function called getcurrentposn which returns the current position in milliseconds, I want to display the result in the TextView in this form mm:ss.
I made a getAsTime function which have returns the time in my app in the digital form i.e. mm:ss.
This is what I tried:
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
long seconds = minutes * 60;
if(millis!=0) {
long totalSeconds = TimeUnit.MILLISECONDS.toSeconds(millis);
if(seconds!=0)
seconds = totalSeconds / seconds;
else seconds=totalSeconds;
}
if(minutes<10 && seconds <10)
return "0"+minutes + ":" +"0"+ seconds;
if(minutes<10 && seconds>=10)
return "0"+minutes + ":" + seconds;
if(minutes>=10 && seconds<10)
return minutes + ":" +"0"+ seconds;
return minutes+":"+seconds;
}
But the problem with that it is doesn't seem to work correctly.
Is there any inbuilt function to do so? if not how do I achieve it the correct way?
Example: 01:00
02:09
First convert the milliseconds to seconds:
int t = millis / 1000;
Then convert those to minutes and seconds
int minutes = t / 60; //since both are ints, you get an int
int seconds = t % 60;
Then return a string formatted like you want it with added padding
return String.format("%02d:%02d", minutes, seconds);
How do I set my time difference on countdown?
I have the time difference from my java code, and all I want for it to do is to countdown.
Here's my java code.
public class time {
public void printDifference( Date endDate){
Date now = new Date();
Date startDate = now;
//milliseconds
long different = endDate.getTime() - startDate.getTime();
System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);
System.out.println("different : " + different);
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
System.out.printf(
"%d days, %d hours, %d minutes, %d seconds%n",
elapsedDays,
elapsedHours, elapsedMinutes, elapsedSeconds);
}
public static void main(String[] args) {
}
}
Assuming that you require a countdown timer in background, say for a quiz application, running a separate thread in the background is a viable solution.
Let me know if you expect something else.
public class time implements Runnable{
private Thread counter;
//making your local varibles members so that they could be used by "counter" thread
private long different, secondsInMilli, minutesInMilli,
hoursInMilli, daysInMilli;
public void printDifference( Date endDate){
Date now = new Date();
Date startDate = now;
//milliseconds
this.different = endDate.getTime() - startDate.getTime();
System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);
System.out.println("different : " + different);
//Initializing your variables
this.secondsInMilli = 1000;
this.minutesInMilli = secondsInMilli * 60;
this.hoursInMilli = minutesInMilli * 60;
this.daysInMilli = hoursInMilli * 24;
this.counter = new Thread(this, "counter");
counter.start();
}
#Override
public void run() {
if(Thread.currentThread().getName().equals("counter")){
try{
long differentCopy = different;
while(differentCopy > 0 ){
//dropping your calculations here
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
System.out.printf("%d days, %d hours, %d minutes, %d seconds\n",
elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
different = differentCopy-=1000;
Thread.sleep(1000);
}
}catch(InterruptedException | HeadlessException e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO code application logic here
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MILLISECOND, 10000);//current time + 10s
Date endDate = calendar.getTime();
time t = new time();
t.printDifference(endDate);
}}
Output:
startDate : Sat Apr 01 19:30:12 IST 2017
endDate : Sat Apr 01 19:30:22 IST 2017
different : 9999
0 days, 0 hours, 0 minutes, 9 seconds
0 days, 0 hours, 0 minutes, 8 seconds
0 days, 0 hours, 0 minutes, 7 seconds
0 days, 0 hours, 0 minutes, 6 seconds
0 days, 0 hours, 0 minutes, 5 seconds
0 days, 0 hours, 0 minutes, 4 seconds
0 days, 0 hours, 0 minutes, 3 seconds
0 days, 0 hours, 0 minutes, 2 seconds
0 days, 0 hours, 0 minutes, 1 seconds
0 days, 0 hours, 0 minutes, 0 seconds
I'm currently trying to convert a long to a remaining time. I have got a
long remaining = XXXX
The long are the milliseconds to a certain date. For example: 3,600,000 should result in int weeks = 0, days = 0, hours = 1, minutes = 0, seconds = 0
how can I convert this long so that I end up with 5 ints:
int weeks;
int days;
int hours;
int minutes;
int seconds;
Thank you in advance!
DirtyDev
First, I suggest defining the number of ms in a second, minute, hour, etc as constants
static final int SECOND = 1000; // no. of ms in a second
static final int MINUTE = SECOND * 60; // no. of ms in a minute
static final int HOUR = MINUTE * 60; // no. of ms in an hour
static final int DAY = HOUR * 24; // no. of ms in a day
static final int WEEK = DAY * 7; // no. of ms in a week
Then, you can use basic division (/) and modulus (%) operations to find what you need.
long remaining = XXXX;
int weeks = (int)( remaining / WEEK);
int days = (int)((remaining % WEEK) / DAY);
int hours = (int)((remaining % DAY) / HOUR);
int minutes = (int)((remaining % HOUR) / MINUTE);
int seconds = (int)((remaining % MINUTE) / SECOND);
Excuse me, I don’t want to criticize too much, still I gather from the other answers that it’s easy to either write code that is hard to read or code with typos that gives an incorrect result. DirtyDev, I am aware that you may not be allowed to use Duration, but for anyone else:
long remaining = 3_600_000;
Duration remainingTime = Duration.ofMillis(remaining);
long days = remainingTime.toDays();
remainingTime = remainingTime.minusDays(days);
long weeks = days / 7;
days %= 7; // or if you prefer, days = days % 7;
long hours = remainingTime.toHours();
remainingTime = remainingTime.minusHours(hours);
long minutes = remainingTime.toMinutes();
remainingTime = remainingTime.minusMinutes(minutes);
long seconds = remainingTime.getSeconds();
System.out.println("" + weeks + " weeks " + days + " days "
+ hours + " hours " + minutes + " minutes " + seconds + " seconds");
This prints:
0 weeks 0 days 1 hours 0 minutes 0 seconds
It’s not perfect, but I believe it’s both readable, correct and robust. Duration was meant for times from hours down to nanoseconds, so we still have to do the weeks “by hand”.
Happy New Year.
This should do what you want.
long inputTimeInMilliseconds = 93800000;
long milliseconds = inputTimeInMilliseconds % 1000;
long seconds = (inputTimeInMilliseconds / 1000) % 60 ;
long minutes = ((inputTimeInMilliseconds / (1000*60)) % 60);
long hours = ((inputTimeInMilliseconds / (1000*60*60)) % 24);
long days = ((inputTimeInMilliseconds / (1000*60*60*24)) % 7);
long weeks = (inputTimeInMilliseconds / (1000*60*60*24*7));
String remainingTime = "time:"+weeks+":"+days+":"+ hours+":"+minutes+":"+seconds+":"+milliseconds;
System.out.println(remainingTime);
I want to use count up timer in android for long hours...
Currently, I am using this code, but after some hours, say after 10 hours, the format goes like 10 : 650 :56 (hh:mm:ss)... for lesser time, it works perfectly...
private Runnable updateTimerMethod = new Runnable() {
public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
int hours = minutes / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
String timer = ("" + String.format("%02d", hours) + " : "
+ String.format("%02d", minutes) + " : "
+ String.format("%02d", seconds));
myHandler.postDelayed(this, 0);
sendLocalBroadcast(timer);
}
};
Your code for minutes is almost right, but you have to modulus it by 60 just like you do for seconds. Otherwise your value is going to still include all the hours.
Use this function:
private static String timeConversion(int totalSeconds) {
final int MINUTES_IN_AN_HOUR = 60;
final int SECONDS_IN_A_MINUTE = 60;
int seconds = totalSeconds % SECONDS_IN_A_MINUTE;
int totalMinutes = totalSeconds / SECONDS_IN_A_MINUTE;
int minutes = totalMinutes % MINUTES_IN_AN_HOUR;
int hours = totalMinutes / MINUTES_IN_AN_HOUR;
return hours + " : " + minutes + " : " + seconds;
}
You can found other solution in:
https://codereview.stackexchange.com/q/62713/69166
I've tried mutliple solutions to this problem but I can't seem to get it.
I have time in decimal format, which is in hours. I want to make it much cleaner by changing it into a DD:HH:MM:SS format.
Example:
10.89 hours == 10 hours, 53 minutes, 40 seconds
EDIT: 10.894945454545455 == 10 hours, 53 minutes, 40 seconds
What I've tried:
int hours = (int) ((finalBuildTime) % 1);
int minutes = (int) ((finalBuildTime * (60*60)) % 60);
int seconds = (int) ((finalBuildTime * 3600) % 60);
return String.format("%s(h) %s(m) %s(s)", hours, minutes, seconds);
Which returned: 0(h) 41(m) 41(s)
Any suggestions?
There is no need to do a modular on minutes.
Your calculation of minutes should just multiply by 60, not (60*60)
double finalBuildTime = 10.89;
int hours = (int) finalBuildTime;
int minutes = (int) (finalBuildTime * 60) % 60;
int seconds = (int) (finalBuildTime * (60*60)) % 60;
System.out.println(String.format("%s(h) %s(m) %s(s)", hours, minutes, seconds));
This code gives you the correct output
10(h) 53(m) 24(s)
I believe your expected output of 40 seconds is incorrect. It should be 24 seconds.
(53*60 + 24)/(60*60) = 0.89
Here is a complete implementation:
package test.std.java;
public class Test {
public static void main(String[] args) {
System.out.println(GetReadableTime(10.89));
}
//Prints outs HH:MM:SS
public static String GetReadableTime(double finalBuildTime){
int hours = (int) Math.floor(finalBuildTime);
int remainderInSeconds = (int)(3600.0* (finalBuildTime - Math.floor(finalBuildTime)) );
int seconds = remainderInSeconds % 60;
int minutes = remainderInSeconds / 60;
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
}
}
First part of rgettman is true :
float finalBuildTime = 10.89;
int hours = (int) finalBuildTime; // 10 ok
But (int) ((10.89 * (60 *60)) / 60) = 683 which is not what you want : it is the direct conversion of finalBuildTime in minutes
int minutes = (int) ((finalBuildTime - hours) * 60); // 53 ok
int seconds = (int) ((finalBuildTime - hours) * 3600 - minutes * 60 + 0.5); // 24 ok
I've added 0.5 for the seconds computation to round to the nearest second rather than truncate. For your example it is no use because your time is an integer number of seconds.
And the number of seconds is 24 seconds = 0.4 minutes
I assume finalBuildTime is a double. You just have to do :
int hours = (int)finalBuildTime;
int minutes = (int)((finalBuildTime - hours) * 60);
int seconds = (int)((((finalBuildTime - hours) * 60) - minutes ) * 60);
(int) rounds down. Therefore, (int)(finalBuildTime) gives you hours.
Since 60 times 60 is 3600, your seconds and minutes calculations are the same.
To find the minutes and seconds, I would observe that the 10.89 hours in your example is 10 hours and 0.89 hours. I would first calculate how many minutes is in 0.89 hours.(the part after the decimal point) That would be 0.89 times 60. Again, since this number should always be less than 60, casting(therefore, rounding down) the number to an (int) gives you minutes. Therefore, for minutes, it is int minutes = (int)((finalBuildTime-hours)*60);
You can keep the rest of your code.
The other answers are correct, but in case it helps you (or someone else who stumbles across this) it is a lot easier to use a library like Joda-Time to do this. Here is a solution to your problem using Joda-Time 2.3:
double buildDurationInHours = 10.89;
long buildDurationInMilliseconds = new Double(DateTimeConstants.MILLIS_PER_HOUR * buildDurationInHours).longValue();
Period period = new Period(buildDurationInMilliseconds);
System.out.println("The build took "
+ period.getDays() + " days, "
+ period.getHours() + " hours, "
+ period.getMinutes() + " minutes, and "
+ period.getSeconds() + " seconds.");
Output:
The build took 0 days, 10 hours, 53 minutes, and 24 seconds.
This way you don't have to deal directly with calculating the hours, minutes, seconds, etc.