Format Stopwatch to display 00:00 - java

I am experimenting with using a stopwatch in one of my apps and I have got it to where it will start counting seconds on start and stop counting on stop. My problem is that it will keep going on after 60 seconds. For example I get 120 seconds if I waited for two minutes.
So my question is how can I make it so once the seconds reached 60 the minutes would be increased by one and the seconds would start over?
So instead of :120 I would get 2:00. Here is the code I have:
final int MSG_START_TIMER = 0;
final int MSG_STOP_TIMER = 1;
final int MSG_UPDATE_TIMER = 2;
Stopwatch timer = new Stopwatch();
final int REFRESH_RATE = 100;
Handler mHandler = new Handler()
{
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MSG_START_TIMER:
timer.start();
mHandler.sendEmptyMessage(MSG_UPDATE_TIMER);
break;
case MSG_UPDATE_TIMER:
tvTextView.setText(":"+ timer.getElapsedTimeSecs());
mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER,REFRESH_RATE);
break;
case MSG_STOP_TIMER:
mHandler.removeMessages(MSG_UPDATE_TIMER);
timer.stop();
tvTextView.setText("");
break;
default:
break;
}
}
};
Also are:
public void start(View v) {
mHandler.sendEmptyMessage(MSG_START_TIMER);
}
public void stop(View v) {
mHandler.sendEmptyMessage(MSG_STOP_TIMER);
}
By the way I looked at this question but his issue was with TimeSpan and if I understand correctly that is different from Stopwatch(correct me if I am wrong). Thanks for your time and effort.

If you start with the time in seconds:
display = String.format("%d:%02d", seconds / 60, seconds % 60);
If you don't want the minutes if they are 0:
if (seconds < 60)
display = String.format("%02d", seconds);
else
display = String.format("%d:%02d", seconds / 60, seconds % 60);

You are just going to have to make a function that creates a string for you. Stash this somewhere reachable.
public String NumToStr(long i){
if (i < 10 ) {
return ("0" + Long.toString(i));
}
return Long.toString(i);
}
this will take any number that is less than 10 and give you a string with a "0" infront of the number.
i.e. send it a 7 and get "07" then send it a 55 and get "55" because it was not less than 10. Finaly send it the seconds 5 and get "05". Now add the strings together,
Hour + ":" + Minute + ":" + Seconds;
and you will get "07:55:05"
next just put in a
if (seconds > 60) {
seconds = 0
minutes++;
}
if (minutes > 60) {
minutes = 0
hours++;
}
if (hours > 12) {
hours = 0
}
With all respect this is basic stuff.
In response to where should you put all of this? Where ever you like. But you need to redo some of your code.
case MSG_UPDATE_TIMER:
long TimePassed;
TimePassed = Seconds;
if (Minutes > 0) {
TimePassed = TimePassed + (60 * Minutes);
}
if (Hours > 0) {
TimePassed = TimePassed + (60 * 60 * Hours);
}
Seconds = (timer.getElapsedTimeSecs()- TimePassed );
if (Seconds > 60) {
Seconds = 0
Minutes++;
}
if (Minutes > 60) {
Minutes = 0
hours++;
}
String timeSecs = NumToStr(Seconds);
String timeMins = NumToStr(Minutes);
String timeHours = NumToStr(Hours);
String Time = timeHours + ":" + timeMins + ":" + timeSecs;
tvTextView.setText(Time);
mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER,REFRESH_RATE);
break;

Related

Stopwatch with sound at a certain time

I have a project that contains a timer, I'm trying to put an alarm or music to play when the time of 12 minutes is reached. How could this be possible? If anyone can help me, I'm a beginner.
Buttons that start, pause, and reset the timer
switch (view.getId()) {
case R.id.bt_control:
if (btControl.getText().equals("Play")) {
this.startService(intent);
cmPasstime.setBase(SystemClock.elapsedRealtime());
cmPasstime.start();
btControl.setText("Stop");
} else if (btControl.getText().equals("Stop")) {
this.stopService(intent);
cmPasstime.stop();
btControl.setText("Play");
} else if (btControl.getText().equals("Play")) {
this.startService(intent);
cmPasstime.start();
btControl.setText("Stop");
}
break;
case R.id.bt_reset:
reset();
break;
}
Stopwatch
#Override
public void onChronometerTick(Chronometer arg0) {
seconds++;
cmPasstime.setText(formatseconds());
}
public String formatseconds() {
String hh = seconds / 3600 > 9 ? seconds / 3600 + "" : "0" + seconds
/ 3600;
String mm = (seconds % 3600) / 60 > 9 ? (seconds % 3600) / 60 + ""
: "0" + (seconds % 3600) / 60;
String ss = (seconds % 3600) % 60 > 9 ? (seconds % 3600) % 60 + ""
: "0" + (seconds % 3600) % 60;
return hh + " : " + mm + " : " + ss;
}
Reset
private void reset() {
tvPasstime.setText("00:00:00");
cmPasstime.setBase(SystemClock.elapsedRealtime());
cmPasstime.stop();
btControl.setText("Play");
}
Try to use Firebase JobDispatcher, this library will schedule job in background to do something at specified time.
https://github.com/firebase/firebase-jobdispatcher-android

Converting seconds to hours, minutes and seconds

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

Creating a java Clock. increasing time

I am creating a clock in Java. I have managed to do some very basic stuff and now i want to implement a function.
If i set the time to i.e. 12:04:59 and use my timeTick Method it will increase the time with 1 second but the problem is it will say the time is 12:04:60 and it doesn't change to 12:05:00.
I've been struggling a while now and i can't really find a solution to it.
My code is below, i hope you can help me,
public class Clock{
public int seconds;
public int minutes;
public int hours;
public Clock ( int InsertSeconds, int InsertMinutes, int InsertHours){
seconds = InsertSeconds;
minutes = InsertMinutes;
hours = InsertHours;
}
public void timeTick(){
seconds = seconds + 1;
}
public String toString(){
return hours + ":" + minutes + ":" + seconds;
}
}
I am not planning to use Imports because i am a beginner it would be great if we can keep it simple.
The problem here lays in the timeTick() function. In a real clock example we have some extra rules for counting. Every time we count to 60 seconds, we add a minute. Every time we count to 60 minutes, we add an hour. So you have to implement these rules.
// lets make some simple code
public void timeTick(){
seconds = seconds + 1; // you can also use seconds++; it means exactly the same thing
if(seconds == 60){
minutes++; // we reached a minute, we need to add a minute
seconds = 0; // we restart our seconds counter
if(minutes == 60){
hours++; // we reached an hour, we need to add an hour
minutes = 0; // we restart our minutes counter
// and so on, if you want to use days (24 h a day) , months ( a bit more difficult ), ...
}
}
}
I hope this will help you, for a starter it might be a good idea to split the second part of code into a function, which deals with this situation. Good luck!
How about this:
public void timeTick () {
seconds++;
while (seconds >= 60) {
minutes++;
seconds-=60;
}
while (minutes >= 60) {
hours++;
minutes-=60;
}
}
Try this :
public void timeTick () {
seconds++;
if (seconds == 60)
{
seconds = 0;
minutes++;
if (minutes == 60)
{
minutes = 0;
hours++;
}
}
}
try this:
public class Clock{
public int seconds;
public int minutes;
public int hours;
public Clock ( int InsertSeconds, int InsertMinutes, int InsertHours){
seconds = InsertSeconds;
minutes = InsertMinutes;
hours = InsertHours;
}
public void timeTick(){
seconds = seconds + 1;
if(seconds==60){
minutes++;
seconds=0;
if(minutes==60){
hours++;
minutes=0;
}
}
}
public String toString(){
return hours + ":" + minutes + ":" + seconds;
}
}
You could check for when you have 60 seconds, then reset seconds to zero, and increase minutes by 1. e.g.
if (the condition you want to check) {
//increase the number of minutes.
//reset number of seconds.
}
If you're entering seconds values of more than 60, you'll need to work out how many minutes that equals using division, and the remaining number of seconds using the modulus operator: % e.g.
seconds = 125;
minutes = seconds / 60; // 2 minutes
remaining_seconds = seconds % 60; // 5 seconds

TimeUtil package in java [duplicate]

I want to record the time using System.currentTimeMillis() when a user begins something in my program. When he finishes, I will subtract the current System.currentTimeMillis() from the start variable, and I want to show them the time elapsed using a human readable format such as "XX hours, XX mins, XX seconds" or even "XX mins, XX seconds" because its not likely to take someone an hour.
What's the best way to do this?
Use the java.util.concurrent.TimeUnit class:
String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
);
Note: TimeUnit is part of the Java 1.5 specification, but toMinutes was added as of Java 1.6.
To add a leading zero for values 0-9, just do:
String.format("%02d min, %02d sec",
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
);
If TimeUnit or toMinutes are unsupported (such as on Android before API version 9), use the following equations:
int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours = (int) ((milliseconds / (1000*60*60)) % 24);
//etc...
Based on #siddhadev's answer, I wrote a function which converts milliseconds to a formatted string:
/**
* Convert a millisecond duration to a string format
*
* #param millis A duration to convert to a string form
* #return A string of the form "X Days Y Hours Z Minutes A Seconds".
*/
public static String getDurationBreakdown(long millis) {
if(millis < 0) {
throw new IllegalArgumentException("Duration must be greater than zero!");
}
long days = TimeUnit.MILLISECONDS.toDays(millis);
millis -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(millis);
millis -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
millis -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
StringBuilder sb = new StringBuilder(64);
sb.append(days);
sb.append(" Days ");
sb.append(hours);
sb.append(" Hours ");
sb.append(minutes);
sb.append(" Minutes ");
sb.append(seconds);
sb.append(" Seconds");
return(sb.toString());
}
long time = 1536259;
return (new SimpleDateFormat("mm:ss:SSS")).format(new Date(time));
Prints:
25:36:259
Using the java.time package in Java 8:
Instant start = Instant.now();
Thread.sleep(63553);
Instant end = Instant.now();
System.out.println(Duration.between(start, end));
Output is in ISO 8601 Duration format: PT1M3.553S (1 minute and 3.553 seconds).
Uhm... how many milliseconds are in a second? And in a minute? Division is not that hard.
int seconds = (int) ((milliseconds / 1000) % 60);
int minutes = (int) ((milliseconds / 1000) / 60);
Continue like that for hours, days, weeks, months, year, decades, whatever.
I would not pull in the extra dependency just for that (division is not that hard, after all), but if you are using Commons Lang anyway, there are the DurationFormatUtils.
Example Usage (adapted from here):
import org.apache.commons.lang3.time.DurationFormatUtils
public String getAge(long value) {
long currentTime = System.currentTimeMillis();
long age = currentTime - value;
String ageString = DurationFormatUtils.formatDuration(age, "d") + "d";
if ("0d".equals(ageString)) {
ageString = DurationFormatUtils.formatDuration(age, "H") + "h";
if ("0h".equals(ageString)) {
ageString = DurationFormatUtils.formatDuration(age, "m") + "m";
if ("0m".equals(ageString)) {
ageString = DurationFormatUtils.formatDuration(age, "s") + "s";
if ("0s".equals(ageString)) {
ageString = age + "ms";
}
}
}
}
return ageString;
}
Example:
long lastTime = System.currentTimeMillis() - 2000;
System.out.println("Elapsed time: " + getAge(lastTime));
//Output: 2s
Note: To get millis from two LocalDateTime objects you can use:
long age = ChronoUnit.MILLIS.between(initTime, LocalDateTime.now())
Either hand divisions, or use the SimpleDateFormat API.
long start = System.currentTimeMillis();
// do your work...
long elapsed = System.currentTimeMillis() - start;
DateFormat df = new SimpleDateFormat("HH 'hours', mm 'mins,' ss 'seconds'");
df.setTimeZone(TimeZone.getTimeZone("GMT+0"));
System.out.println(df.format(new Date(elapsed)));
Edit by Bombe: It has been shown in the comments that this approach only works for smaller durations (i.e. less than a day).
Just to add more info
if you want to format like: HH:mm:ss
0 <= HH <= infinite
0 <= mm < 60
0 <= ss < 60
use this:
int h = (int) ((startTimeInMillis / 1000) / 3600);
int m = (int) (((startTimeInMillis / 1000) / 60) % 60);
int s = (int) ((startTimeInMillis / 1000) % 60);
I just had this issue now and figured this out
Shortest solution:
Here's probably the shortest which also deals with time zones.
System.out.printf("%tT", millis-TimeZone.getDefault().getRawOffset());
Which outputs for example:
00:18:32
Explanation:
%tT is the time formatted for the 24-hour clock as %tH:%tM:%tS.
%tT also accepts longs as input, so no need to create a Date. printf() will simply print the time specified in milliseconds, but in the current time zone therefore we have to subtract the raw offset of the current time zone so that 0 milliseconds will be 0 hours and not the time offset value of the current time zone.
Note #1: If you need the result as a String, you can get it like this:
String t = String.format("%tT", millis-TimeZone.getDefault().getRawOffset());
Note #2: This only gives correct result if millis is less than a day because the day part is not included in the output.
I think the best way is:
String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toSeconds(length)/60,
TimeUnit.MILLISECONDS.toSeconds(length) % 60 );
Revisiting #brent-nash contribution, we could use modulus function instead of subtractions and use String.format method for the result string:
/**
* Convert a millisecond duration to a string format
*
* #param millis A duration to convert to a string form
* #return A string of the form "X Days Y Hours Z Minutes A Seconds B Milliseconds".
*/
public static String getDurationBreakdown(long millis) {
if (millis < 0) {
throw new IllegalArgumentException("Duration must be greater than zero!");
}
long days = TimeUnit.MILLISECONDS.toDays(millis);
long hours = TimeUnit.MILLISECONDS.toHours(millis) % 24;
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) % 60;
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) % 60;
long milliseconds = millis % 1000;
return String.format("%d Days %d Hours %d Minutes %d Seconds %d Milliseconds",
days, hours, minutes, seconds, milliseconds);
}
Joda-Time
Using Joda-Time:
DateTime startTime = new DateTime();
// do something
DateTime endTime = new DateTime();
Duration duration = new Duration(startTime, endTime);
Period period = duration.toPeriod().normalizedStandard(PeriodType.time());
System.out.println(PeriodFormat.getDefault().print(period));
For those who looking for Kotlin code:
fun converter(millis: Long): String =
String.format(
"%02d : %02d : %02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(
TimeUnit.MILLISECONDS.toHours(millis)
),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millis)
)
)
Sample output: 09 : 10 : 26
My simple calculation:
String millisecToTime(int millisec) {
int sec = millisec/1000;
int second = sec % 60;
int minute = sec / 60;
if (minute >= 60) {
int hour = minute / 60;
minute %= 60;
return hour + ":" + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second);
}
return minute + ":" + (second < 10 ? "0" + second : second);
}
Happy coding :)
Firstly, System.currentTimeMillis() and Instant.now() are not ideal for timing. They both report the wall-clock time, which the computer doesn't know precisely, and which can move erratically, including going backwards if for example the NTP daemon corrects the system time. If your timing happens on a single machine then you should instead use System.nanoTime().
Secondly, from Java 8 onwards java.time.Duration is the best way to represent a duration:
long start = System.nanoTime();
// do things...
long end = System.nanoTime();
Duration duration = Duration.ofNanos(end - start);
System.out.println(duration); // Prints "PT18M19.511627776S"
System.out.printf("%d Hours %d Minutes %d Seconds%n",
duration.toHours(), duration.toMinutes() % 60, duration.getSeconds() % 60);
// prints "0 Hours 18 Minutes 19 Seconds"
for Android below API 9
(String.format("%d hr %d min, %d sec", millis/(1000*60*60), (millis%(1000*60*60))/(1000*60), ((millis%(1000*60*60))%(1000*60))/1000))
For small times, less than an hour, I prefer:
long millis = ...
System.out.printf("%1$TM:%1$TS", millis);
// or
String str = String.format("%1$TM:%1$TS", millis);
for longer intervalls:
private static final long HOUR = TimeUnit.HOURS.toMillis(1);
...
if (millis < HOUR) {
System.out.printf("%1$TM:%1$TS%n", millis);
} else {
System.out.printf("%d:%2$TM:%2$TS%n", millis / HOUR, millis % HOUR);
}
Here is an answer based on Brent Nash answer, Hope that helps !
public static String getDurationBreakdown(long millis)
{
String[] units = {" Days ", " Hours ", " Minutes ", " Seconds "};
Long[] values = new Long[units.length];
if(millis < 0)
{
throw new IllegalArgumentException("Duration must be greater than zero!");
}
values[0] = TimeUnit.MILLISECONDS.toDays(millis);
millis -= TimeUnit.DAYS.toMillis(values[0]);
values[1] = TimeUnit.MILLISECONDS.toHours(millis);
millis -= TimeUnit.HOURS.toMillis(values[1]);
values[2] = TimeUnit.MILLISECONDS.toMinutes(millis);
millis -= TimeUnit.MINUTES.toMillis(values[2]);
values[3] = TimeUnit.MILLISECONDS.toSeconds(millis);
StringBuilder sb = new StringBuilder(64);
boolean startPrinting = false;
for(int i = 0; i < units.length; i++){
if( !startPrinting && values[i] != 0)
startPrinting = true;
if(startPrinting){
sb.append(values[i]);
sb.append(units[i]);
}
}
return(sb.toString());
}
long startTime = System.currentTimeMillis();
// do your work...
long endTime=System.currentTimeMillis();
long diff=endTime-startTime;
long hours=TimeUnit.MILLISECONDS.toHours(diff);
diff=diff-(hours*60*60*1000);
long min=TimeUnit.MILLISECONDS.toMinutes(diff);
diff=diff-(min*60*1000);
long seconds=TimeUnit.MILLISECONDS.toSeconds(diff);
//hour, min and seconds variables contains the time elapsed on your work
This is easier in Java 9:
Duration elapsedTime = Duration.ofMillis(millisDiff );
String humanReadableElapsedTime = String.format(
"%d hours, %d mins, %d seconds",
elapsedTime.toHours(),
elapsedTime.toMinutesPart(),
elapsedTime.toSecondsPart());
This produces a string like 0 hours, 39 mins, 9 seconds.
If you want to round to whole seconds before formatting:
elapsedTime = elapsedTime.plusMillis(500).truncatedTo(ChronoUnit.SECONDS);
To leave out the hours if they are 0:
long hours = elapsedTime.toHours();
String humanReadableElapsedTime;
if (hours == 0) {
humanReadableElapsedTime = String.format(
"%d mins, %d seconds",
elapsedTime.toMinutesPart(),
elapsedTime.toSecondsPart());
} else {
humanReadableElapsedTime = String.format(
"%d hours, %d mins, %d seconds",
hours,
elapsedTime.toMinutesPart(),
elapsedTime.toSecondsPart());
}
Now we can have for example 39 mins, 9 seconds.
To print minutes and seconds with leading zero to make them always two digits, just insert 02 into the relevant format specifiers, thus:
String humanReadableElapsedTime = String.format(
"%d hours, %02d mins, %02d seconds",
elapsedTime.toHours(),
elapsedTime.toMinutesPart(),
elapsedTime.toSecondsPart());
Now we can have for example 0 hours, 39 mins, 09 seconds.
for correct strings ("1hour, 3sec", "3 min" but not "0 hour, 0 min, 3 sec") i write this code:
int seconds = (int)(millis / 1000) % 60 ;
int minutes = (int)((millis / (1000*60)) % 60);
int hours = (int)((millis / (1000*60*60)) % 24);
int days = (int)((millis / (1000*60*60*24)) % 365);
int years = (int)(millis / 1000*60*60*24*365);
ArrayList<String> timeArray = new ArrayList<String>();
if(years > 0)
timeArray.add(String.valueOf(years) + "y");
if(days > 0)
timeArray.add(String.valueOf(days) + "d");
if(hours>0)
timeArray.add(String.valueOf(hours) + "h");
if(minutes>0)
timeArray.add(String.valueOf(minutes) + "min");
if(seconds>0)
timeArray.add(String.valueOf(seconds) + "sec");
String time = "";
for (int i = 0; i < timeArray.size(); i++)
{
time = time + timeArray.get(i);
if (i != timeArray.size() - 1)
time = time + ", ";
}
if (time == "")
time = "0 sec";
If you know the time difference would be less than an hour, then you can use following code:
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c2.add(Calendar.MINUTE, 51);
long diff = c2.getTimeInMillis() - c1.getTimeInMillis();
c2.set(Calendar.MINUTE, 0);
c2.set(Calendar.HOUR, 0);
c2.set(Calendar.SECOND, 0);
DateFormat df = new SimpleDateFormat("mm:ss");
long diff1 = c2.getTimeInMillis() + diff;
System.out.println(df.format(new Date(diff1)));
It will result to: 51:00
This answer is similar to some answers above. However, I feel that it would be beneficial because, unlike other answers, this will remove any extra commas or whitespace and handles abbreviation.
/**
* Converts milliseconds to "x days, x hours, x mins, x secs"
*
* #param millis
* The milliseconds
* #param longFormat
* {#code true} to use "seconds" and "minutes" instead of "secs" and "mins"
* #return A string representing how long in days/hours/minutes/seconds millis is.
*/
public static String millisToString(long millis, boolean longFormat) {
if (millis < 1000) {
return String.format("0 %s", longFormat ? "seconds" : "secs");
}
String[] units = {
"day", "hour", longFormat ? "minute" : "min", longFormat ? "second" : "sec"
};
long[] times = new long[4];
times[0] = TimeUnit.DAYS.convert(millis, TimeUnit.MILLISECONDS);
millis -= TimeUnit.MILLISECONDS.convert(times[0], TimeUnit.DAYS);
times[1] = TimeUnit.HOURS.convert(millis, TimeUnit.MILLISECONDS);
millis -= TimeUnit.MILLISECONDS.convert(times[1], TimeUnit.HOURS);
times[2] = TimeUnit.MINUTES.convert(millis, TimeUnit.MILLISECONDS);
millis -= TimeUnit.MILLISECONDS.convert(times[2], TimeUnit.MINUTES);
times[3] = TimeUnit.SECONDS.convert(millis, TimeUnit.MILLISECONDS);
StringBuilder s = new StringBuilder();
for (int i = 0; i < 4; i++) {
if (times[i] > 0) {
s.append(String.format("%d %s%s, ", times[i], units[i], times[i] == 1 ? "" : "s"));
}
}
return s.toString().substring(0, s.length() - 2);
}
/**
* Converts milliseconds to "x days, x hours, x mins, x secs"
*
* #param millis
* The milliseconds
* #return A string representing how long in days/hours/mins/secs millis is.
*/
public static String millisToString(long millis) {
return millisToString(millis, false);
}
There is a problem. When milliseconds is 59999, actually it is 1 minute but it will be computed as 59 seconds and 999 milliseconds is lost.
Here is a modified version based on previous answers, which can solve this loss:
public static String formatTime(long millis) {
long seconds = Math.round((double) millis / 1000);
long hours = TimeUnit.SECONDS.toHours(seconds);
if (hours > 0)
seconds -= TimeUnit.HOURS.toSeconds(hours);
long minutes = seconds > 0 ? TimeUnit.SECONDS.toMinutes(seconds) : 0;
if (minutes > 0)
seconds -= TimeUnit.MINUTES.toSeconds(minutes);
return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);
}
I have covered this in another answer but you can do:
public static Map<TimeUnit,Long> computeDiff(Date date1, Date date2) {
long diffInMillies = date2.getTime() - date1.getTime();
List<TimeUnit> units = new ArrayList<TimeUnit>(EnumSet.allOf(TimeUnit.class));
Collections.reverse(units);
Map<TimeUnit,Long> result = new LinkedHashMap<TimeUnit,Long>();
long milliesRest = diffInMillies;
for ( TimeUnit unit : units ) {
long diff = unit.convert(milliesRest,TimeUnit.MILLISECONDS);
long diffInMilliesForUnit = unit.toMillis(diff);
milliesRest = milliesRest - diffInMilliesForUnit;
result.put(unit,diff);
}
return result;
}
The output is something like Map:{DAYS=1, HOURS=3, MINUTES=46, SECONDS=40, MILLISECONDS=0, MICROSECONDS=0, NANOSECONDS=0}, with the units ordered.
It's up to you to figure out how to internationalize this data according to the target locale.
DurationFormatUtils.formatDurationHMS(long)
I modified #MyKuLLSKI 's answer and added plurlization support. I took out seconds because I didn't need them, though feel free to re-add it if you need it.
public static String intervalToHumanReadableTime(int intervalMins) {
if(intervalMins <= 0) {
return "0";
} else {
long intervalMs = intervalMins * 60 * 1000;
long days = TimeUnit.MILLISECONDS.toDays(intervalMs);
intervalMs -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(intervalMs);
intervalMs -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(intervalMs);
StringBuilder sb = new StringBuilder(12);
if (days >= 1) {
sb.append(days).append(" day").append(pluralize(days)).append(", ");
}
if (hours >= 1) {
sb.append(hours).append(" hour").append(pluralize(hours)).append(", ");
}
if (minutes >= 1) {
sb.append(minutes).append(" minute").append(pluralize(minutes));
} else {
sb.delete(sb.length()-2, sb.length()-1);
}
return(sb.toString());
}
}
public static String pluralize(long val) {
return (Math.round(val) > 1 ? "s" : "");
}
Use java.util.concurrent.TimeUnit, and use this simple method:
private static long timeDiff(Date date, Date date2, TimeUnit unit) {
long milliDiff=date2.getTime()-date.getTime();
long unitDiff = unit.convert(milliDiff, TimeUnit.MILLISECONDS);
return unitDiff;
}
For example:
SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
Date firstDate = sdf.parse("06/24/2017 04:30:00");
Date secondDate = sdf.parse("07/24/2017 05:00:15");
Date thirdDate = sdf.parse("06/24/2017 06:00:15");
System.out.println("days difference: "+timeDiff(firstDate,secondDate,TimeUnit.DAYS));
System.out.println("hours difference: "+timeDiff(firstDate,thirdDate,TimeUnit.HOURS));
System.out.println("minutes difference: "+timeDiff(firstDate,thirdDate,TimeUnit.MINUTES));
System.out.println("seconds difference: "+timeDiff(firstDate,thirdDate,TimeUnit.SECONDS));
This topic has been well covered, I just wanted to share my functions perhaps you can make use of these rather than importing an entire library.
public long getSeconds(ms) {
return (ms/1000%60);
}
public long getMinutes(ms) {
return (ms/(1000*60)%60);
}
public long getHours(ms) {
return ((ms/(1000*60*60))%24);
}

Java countdown time doesnt display correct time

Im currently working on a project that has a simple countdown where the user enters a time and it starts counting down from that time. it is GUI based and displays the countdown to the user. ive managed to make it count down but its not displaying correctly.
For example, if i enter 1min 30secs, it counts down from 1:30, to 1:01 then incorrectly displays 0:00 then it counts down from 0:59. it is correct until i get to a point where the minutes change to seconds and it doesnt display correctly for 1 second.
Countdown class:
class countdownClass implements ActionListener { //counts down then beeps
int counter;
public countdownClass(int counter) {
this.counter = counter;
}
public void actionPerformed(ActionEvent sc) { //counts down every second
counter--;
if(counter >= 1) { //a simple countdown
if(counter > 9){ //this statement is for aestetic purpose as before
seconds.setText("" + counter);
}else{
seconds.setText("0" + counter);
}
}
else if(minutesNum >= 1 && counter <= 0){ //if minute exists carry on the countdown
// 1:01 - > 1:00 - > 0:59
int minuteUpdate = minutesNum - 1;
if (minutesNum < 9){
minutes.setText("0" + Integer.toString(minutesNum));
seconds.setText("00");
if(counter <= 0){
minutes.setText("0" + Integer.toString(minuteUpdate));
}
}else{
minutes.setText("0" + Integer.toString(minutesNum));
seconds.setText("0" + Integer.toString(00));
minutes.setText("0" + Integer.toString(minuteUpdate));
}
counter = 60;
}
else if(minutesNum == 0 && counter == 0) { //once the countdown ends plays a beep
timer.stop();
seconds.setText("00");
finish.setText("Time is up!");
start.setEnabled(true);
try{
clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File("ring.wav")));
clip.start();
soundPlay = true;
}
catch (Exception e){
e.printStackTrace(System.out);
}
}
}
}
could you please help me identify where the code is going wrong and provide me with a fix for it.
Thanks for your time.
Your code looks a little over-engineered. Just convert the user-entered time into seconds and decrement that value every second. Then print the result with something like:
private static String secondsToMinutesAndSeconds(int time) {
int minutes = time / 60;
int seconds = time % 60;
return String.format("%02d:%02d", minutes, seconds);
}
E.g. 1 minute 30 seconds is just 90 seconds. Pass 90 into the above method and you get 01:30. If you need the minute and second values individually, it should be easy enough to edit that code example.
It appears that the minutes digit displayed is minuteUdpate which is set to one less than MinutesNum.
else if(minutesNum >= 1 && counter <= 0){ //if minute exists carry on the countdown
// 1:01 - > 1:00 - > 0:59
int minuteUpdate = minutesNum - 1;
if (minutesNum < 9){
minutes.setText("0" + Integer.toString(minutesNum));
seconds.setText("00");
if(counter <= 0){
minutes.setText("0" + Integer.toString(minuteUpdate));
}
}else{
minutes.setText("0" + Integer.toString(minutesNum));
seconds.setText("0" + Integer.toString(00));
minutes.setText("0" + Integer.toString(minuteUpdate));
}
counter = 60;
minutesNum is less than 9, and counter = 0 for 1:00, so this line should be done:
minutes.setText("0" + Integer.toString(minuteUpdate));
I'd remove that if block. The minutes number should not be decremented before the seconds number is changed to 60.
For me, your code is useless complex.
What you should do:
Use two variables to save minutes and seconds.
The start value of there two variables will be what the users put (Examples if its 3:50, minutes will be 3, second will be 50)
Start a timer every second, scale 1 second from counter and if seconds is <= 0, scale minutes and put seconds to 59. If minutes is 0 and seconds.
It's just a concept to show my idea:
private static int minutes = 0;
private static int seconds = 5;
private static Timer timer;
private static TimerTask task = new TimerTask()
{
#Override
public void run()
{
if (countdown())
System.out.println(minutes + ":" + seconds);
else
{
System.out.println("Finish!");
timer.cancel();
}
}
};
private static boolean countdown()
{
seconds --;
if (seconds < 0)
{
minutes--;
seconds = 59;
if (minutes == -1)
{
return false;
}
}
return true;
}
public static void main(String[] args)
{
timer = new Timer();
timer.schedule(task, 0, 1000);
}
You could work with seconds only and avoid the minutes variable.
(Example: 1 min => 60 seconds; and then use a method to fix it using division)
It's my first time i use ScheduledThreadPoolExecutor so it could not be perfect.
private static int minutes = 0;
private static int seconds = 5;
private static ScheduledThreadPoolExecutor executorService;
private static boolean countdown()
{
seconds --;
if (seconds < 0)
{
minutes--;
seconds = 59;
if (minutes == -1)
{
return false;
}
}
return true;
}
private static Runnable task = new Runnable()
{
#Override
public void run()
{
if (countdown())
System.out.println(minutes + ":" + seconds);
else
{
System.out.println("Finish!");
// by throwing an exception we will stop the countdown
throw new RuntimeException();
}
}
};
public static void main(String[] args)
{
executorService = new ScheduledThreadPoolExecutor(1);
executorService.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
try
{
// schedulate again the countdown
System.out.println("Wait 7 seconds");
Thread.sleep(7000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Schedulate again");
// since we work with the original values
// we must reset it
minutes = 0;
seconds = 5;
executorService.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
}
Remember: this code don't work if multi threads calls countdown method!
Anyway, you can use executorService.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS); to restore the countdown.

Categories