How to build stopwatch with hundreth of a second precision? - java

I'm trying to build a stopwatch with hundreth of second precision. I have this code that runs every 10 milliseconds but I am having trouble converting it to a 0(min):0(sec):00 format.
timer.post(new Runnable() {
#Override
public void run() {
time += 1;
txtView.setText(convertTimeToText(time));
timer.postDelayed(this, 10);
}
});
private String convertTimeToText(int time) {
String convertedTime = time / 6000 + ":" + (time / 100) % 60
+ ":" + (time / 10) % 10 + time % 10;
return convertedTime;
}
I need help with the convertTimeToText(int time){} that formats time.
EDIT:
Thanks to Ole V.V. and WJS for the formatting answer and how to fix the delay I was having, this is the code I came up with if anybody needs it, so far it works well, maybe using System.nanoTime() will get you more accurate results but for my use its fine.
public void start(){
final long timeMillisWhenStarted = System.currentTimeMillis();
if(!isRunning){
isRunning = true;
timer.post(new Runnable() {
#Override
public void run() {
long millisNow = System.currentTimeMillis();
long time = millisNow - timeMillisWhenStarted;
yourtxtView.setText(convertTimeToText(time));
timer.postDelayed(this, 10);
}
});
}
}
private String convertTimeToText(long time){
long hundredths = time / 10;
long sec = hundredths / 100;
long min = sec / 60;
return String.format("%02d:%02d.%02d", min % 60, sec % 60, hundredths % 100);
}

See if this helps. The remainders weren't being computed correctly.
For 12340 hundreds of seconds that would be 123.40 seconds
so 12340 / 6000 = 2 for minutes
12340 % 6000 gets what's left which is 340
so 340 /100 = 3 seconds
leaving 340 % 100= 40 hundredths
public static void main(String[] args) {
// n = 16 mins 4 seconds and 99 hundredths
int n = (16 * 6000) + (4 * 100) + 99;
System.out.println(convertTimeToText(n));
}
private static String convertTimeToText(int time) {
int mins = time / 6000;
time %= 6000; // get remaining hundredths
int seconds = time / 100;
int hundredths = time %= 100; // get remaining hundredths
// format the time. The leading 0's mean to pad single
// digits on the left with 0. The 2 is a field width
return String.format("%02d:%02d:%02d", mins, seconds,
hundredths);
}
This prints
16:04:99

Use the standard library
Your timer isn’t accurate. Your observed delay comes from there. Read the time off some clock each time rather than adding 1/100 second. Use for example System.currentTimeMillis(), System.nanoTime() or Instant.now(). Keep the reading from when your stopwatch was started and subtract for getting the current stopwatch value.
Should performing a system call 100 times per second be too expensive (which I don’t expect), do it for example for every 30 ticks to adjust your stopwatch back to the correct time.
Next if you are using Java 9 or higher, use the Duration class for converting from the the time (whether in milliseconds or nanoseconds) to minutes and seconds. If you do the conversion by hand as you tried, it’s error-prone and hard to read, which — I believe — was one reason for your question.
For example:
long millisNow = System.currentTimeMillis();
Duration time = Duration.ofMillis(millisNow - timeMillisWhenStarted);
String convertedTime = String.format("%d:%02d.%02d",
time.toMinutes(), time.toSecondsPart(), time.toMillisPart() / 10);

Related

How should the moveForward(lostTime) give the correct output when the hour and minute variables go below zero?

I knew the method of delaying by minutes, for instance, from 22:50 to 2:10. I inserted 200 in the parameter of delay method, I am concerned that the method of moving the time forward is not working as I attempted by setting the time 1:20 and moving 100 minutes (1 hour and 40 minutes) forward to 23:40. As I tried to run the code, the output displayed 1:40 after moving the time forward. Which line was wrong in the method of moveForward(int lostMinute)?
class Time
{
private int hour; // between 0 - 23
private int minute; // between 0 - 59
public Time()
{
this(0, 0);
}
public Time(int hr, int min)
{
hour = (hr >= 0 && hr < 24) ? hr : 0;
minute = (min >= 0 && min < 60) ? min : 0;
}
public int getHour()
{
return hour;
}
public int getMinute()
{
return minute;
}
public void setHour(int hour)
{
this.hour = (hour >= 0 && hour < 24) ? hour : 0;
public void setMinute(int minute)
{
this.minute = (minute >= 0 && minute < 60) ? minute : 0;
}
public String toString()
{
return String.format("%02d:%02d", hour, minute);
}
public void delay(int newMinute)
{
minute = minute + newMinute;
if(minute >= 60)
{
// (minute / 60) is an integer division and truncates the remainder, which refers to (minute % 60)
hour = hour + (minute / 60);
minute = minute % 60;
if(hour >= 24)
{
hour = hour % 24;
}
}
}
public void moveForward(int lostMinute)
{
if(minute < lostMinute)
{
hour = hour - ((60 + minute) / 60);
minute = (minute + 60) % 60;
if(hour < 0)
{
hour = (24 + hour) % 24;
}
}
else
{
minute = minute - lostMinute;
}
}
}
I saw that delay() is working correctly while moveForward() is not. To make the time notation clearer for sorting, I used String.format("%02d:%02d") to indicate the time between 00:00 and 23:59. Please note that I am not using import java.util.Calender; or 'import java.util.Date; because part of my project consists of sorting an array by just hours and then minutes. For instance, if we are trying to create the bus terminal project, we assume that the date and calendar do not matter in schedule.
public class MainTime
{
public static void main(String[] args)
{
Time t1 = new Time(23:50);
Time t2 = new Time(1:20);
Time t3 = new Time(4:50);
Time t4 = new Time(18:30);
Time t5 = new Time(14:15);
t1.delay(200);
t2.moveForward(100);
t3.delay(100);
t4.moveForward(20);
t5.moveForward(160);
System.out.println(t1.toString());
System.out.println(t2.toString());
System.out.println(t3.toString());
System.out.println(t4.toString());
System.out.println(t5.toString());
}
}
The constraints are when the change in time is greater than the minute in parameter and when the hour is going to zero. When I ran the code in NetBeans, t1 had 2:10 when I added 200 into 23:50 in delay(newMinute) method; t2 had 1:40 when I subtracted 100 from 1:20 in moveForward(lostMinute) method; t3 had 6:30 when I added 100 into 4:50 in delay(newMinute); t4 had 18:10 when I subtracted 20 from 18:30 in moveForward(lostMinute); t5 had 14:-25 when I subtracted 160 from 14:15 in moveForward(lostMinute). The variables t2 and t5 after execution should actually be 23:40 and 11:35, respectively.
Please determine which lines in public void moveForward(int lostMinute) make the improper output after subtracting the minutes from given time.
In case the minute goes to zero, 60 and modulo notation % could be useful; in case the hour goes to zero, 24 and modulo notation % could be useful. I hope for the moveForward(lostMinute) to work well in the cases when minute < 0 and when hour < 0.
java.time
LocalTime t1 = LocalTime.of(23, 50);
t1 = t1.plusMinutes(200);
System.out.println(t1.toString()); // 03:10
LocalTime t2 = LocalTime.of(1, 20);
t2 = t2.minusMinutes(100);
System.out.println(t2.toString()); // 23:40
LocalTime t3 = LocalTime.of(4, 50);
t3 = t3.plusMinutes(100);
System.out.println(t3.toString()); // 06:30
LocalTime t4 = LocalTime.of(18, 30);
t4 = t4.minusMinutes(20);
System.out.println(t4.toString()); // 18:10
LocalTime t5 = LocalTime.of(14, 15);
t5 = t5.minusMinutes(160);
System.out.println(t5.toString()); // 11:35
Output is given as comments. I think it is what you wanted. So don’t reinvent the wheel. Instead of rolling your own Time class, use LocalTime. It’s there for you to use, it has been developed and tested for you. LocalTime is a time of day in the interval from 00:00 to 23:59:59.999999999. Except that it include seconds and fraction of second it coincides with your interval. If you never set the seconds to something other than 0, they won’t be printed through the toString method. Also LocalTime implements Comparable, so sorting is straightforward.
Be aware that a LocalTime object is immutable, so instead of mutator methods it has methods that return a new LocalTime object with the new value. This is already demonstrated with plusMinutes and minusMinutes above. Also instead of myLocalTime.setHour(17); you need myLocalTime = myLocalTime.withHour(17);, etc.
What went wrong in your code?
Your moveForward method seems to be handling the hour correctly in the case where it is to be moved back to the previous hour, for example from 14:05 to 13:55 or from 14:55 to 13:05. In this case you are never subtracting lostMinutes, which I think you should somehow. When minute is 0–59, then ((60 + minute) / 60) will always be 1, so you are always subtracting exactly 1 hour, never 2 or more.
Genrally the expected ranges of the arguments to both delay and moveForward are unclear. I think they should have been documented and the arguments validated against the documented limits.
Link
Oracle tutorial: Date Time explaining how to use java.time.

Rounding off decimals at a particular decimal number java

How to get correct time when subtracting decimal numbers. I wanted to subtract numbers as if it were tiime. But time only goes to 60 minutes whereas numbers to 100. How could I put some sort of restriction on the decimal places to make it like time.
Here's one way. The elapsed time is in milliseconds, as that is what you have when you subtract a start System.currentTimeMillis() from a finish System.currentTimeMillis().
public String formatElapsedTime(long elapsedTime) {
int centiseconds = (int) (((elapsedTime % 1000L) + 5L) / 10L);
centiseconds %= 10;
int seconds = (int) (elapsedTime / 1000L);
int minutes = seconds / 60;
seconds -= minutes * 60;
int hours = minutes / 60;
minutes -= hours * 60;
if (hours > 0) {
return String.format("%2d:%02d:%02d.%02d", hours, minutes, seconds,
centiseconds);
} else if (minutes > 0) {
return String.format("%2d:%02d.%02d", minutes, seconds,
centiseconds);
} else {
return String.format("%2d.%02d", seconds, centiseconds);
}
}
You probably don’t need to display centiseconds when you’re measuring elapsed time in hours.
Because my format strings start with “%2d”, seconds, minutes, and hours less than 10 have a space as the first character of the string. I find this keeps the display from jumping so much. If you don’t want the leading space, change the format String to “%1d”.
You can calculate days as well, for really long elapsed times. You would probably want a format like “3 days, 6 hours, 25 minutes, and 13 seconds”. Or not.

JAVA convert minutes into default time [hh:mm:ss]

what is the easiest and fastest way to convert minutes (double) to default time hh:mm:ss
for example I used this code in python and it's working
time = timedelta(minutes=250.0)
print time
result:
4:10:00
is there a java library or a simple code can do it?
EDIT: To show the seconds as SS you can make an easy custom formatter variable to pass to the String.format() method
EDIT: Added logic to add one minute and recalculate seconds if the initial double value has the number value after the decimal separator greater than 59.
EDIT: Noticed loss of precision when doing math on the double (joy of working with doubles!) seconds, so every now and again it would not be the correct value. Changed code to properly calculate and round it. Also added logic to treat cases when minutes and hour overflow because of cascading from seconds.
Try this (no external libraries needed)
public static void main(String[] args) {
final double t = 1304.00d;
if (t > 1440.00d) //possible loss of precision again
return;
int hours = (int)t / 60;
int minutes = (int)t % 60;
BigDecimal secondsPrecision = new BigDecimal((t - Math.floor(t)) * 100).setScale(2, RoundingMode.HALF_UP);
int seconds = secondsPrecision.intValue();
boolean nextDay = false;
if (seconds > 59) {
minutes++; //increment minutes by one
seconds = seconds - 60; //recalculate seconds
}
if (minutes > 59) {
hours++;
minutes = minutes - 60;
}
//next day
if (hours > 23) {
hours = hours - 24;
nextDay = true;
}
//if seconds >=10 use the same format as before else pad one zero before the seconds
final String myFormat = seconds >= 10 ? "%d:%02d:%d" : "%d:%02d:0%d";
final String time = String.format(myFormat, hours, minutes, seconds);
System.out.print(time);
System.out.println(" " + (nextDay ? "The next day" : "Current day"));
}
Of course this can go on and on, expanding on this algorithm to generalize it. So far it will work until the next day but no further, so we could limit the initial double to that value.
if (t > 1440.00d)
return;
Using Joda you can do something like:
import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;
final Period a = Period.seconds(25635);
final PeriodFormatter hoursMinutes = new PeriodFormatterBuilder().appendHours().appendSuffix(" hour", " hours")
.appendSeparator(" and ").appendMinutes().appendSuffix(" minute", " minutes").appendSeparator(" and ")
.appendSeconds().appendSuffix(" second", " seconds").toFormatter();
System.out.println(hoursMinutes.print(a.normalizedStandard()));
//Accept minutes from user and return time in HH:MM:SS format
private String convertTime(long time)
{
String finalTime = "";
long hour = (time%(24*60)) / 60;
long minutes = (time%(24*60)) % 60;
long seconds = time / (24*3600);
finalTime = String.format("%02d:%02d:%02d",
TimeUnit.HOURS.toHours(hour) ,
TimeUnit.MINUTES.toMinutes(minutes),
TimeUnit.SECONDS.toSeconds(seconds));
return finalTime;
}

Convert int to time Java

I have an int counter that starts at 600 and in a Runnable and is increased by 1.
600 represents 6 am, and 2400 represents midnight.
This int is compared to a int received from an API in the same format.
I need to compare them both; however, the problem is my int has 100 mins in an hour at the moment, so as time goes on it gets more and more out of time.
Is there a way to convert the int counter to a time format? (The Java format of 18000000 = 6am doesn't work)
Cheers Phil
Dave Newton is right by saying its just math. Your integer time is composed by two components, hours and minutes (which is easy to read but difficult to calculate).
int time = 600;
int hours = time / 100;
int minutes = (time - hours * 100) % 60;
So you can't just increment your time (time++), because you end up with a houndred minutes per hour as you wrote. Use this method instead:
int incrementTime(int time) {
time++;
int hours = time / 100;
int minutes = (time - hours * 100) % 60;
if (minutes == 0) hours++;
return hours * 100 + minutes;
}
You can try it out:
time = 600;
for (int i=0; i < 120; i++) {
time = incrementTime(time);
System.out.println(time);
}
If you do really need to transform your 0 --> 2400 to a "time format", you might use:
hour = uTime/100
uMinutes = uTime - hour*100
normalMinutes = (60*uMinutes)/100
Then go about changing to "long" and milliseconds for use elsewhere.
Java already has a way to convert a datetime to a number and vice versa.
//Get integer representation of time
Calendar rightNow = Calendar.getInstance();
long integerRepresentation = rightNow.getTimeInMillis();
//Set time to integer reprsentation
Calendar calendar2 = Calendar.getInstance();
calendar2.setTimeInMillis(integerRepresentation);

Clever ways to restrict integer from 100 based to 60 based for time values? (e.g.60 seconds in a minute)

I am required to store a time value in an integer in the format of HHMMSS. this time value is incrementing every second (basically a custom clock). however, since integers are naturally 10 based, I must implement a large cumbersome logic that extracts each digits and checks for 60 seconds in a minutes, 60 minutes in an hour and 24 hours a day. I wonder if there is some clever ways to do it without a massive if/else if chunk.
You can use the modulus operator to pick out each of the components of a single seconds counter:
int totalSeconds;
...
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = (totalSeconds / 3600);
Then you can just increment a single seconds counter and extract each of the components.
My suggestion would be to implement a CustomClock class, which could look something like:
public class CustomClock {
private int hour;
private int minute;
private int second;
public CustomClock(int hour, int minute, int second) {
this.hour = hour;
// ...
}
public void increment() {
second = second + 1)%60;
if (second == 0) minute = (minute + 1)%60;
if (minute == 0) hour = (hour + 1)%24;
}
}
Thus taking advantage of the mod operator (%) to compute arbitrary base numbers.

Categories