Reputation: 144
Scanner input2 = new Scanner(System.in);
System.out.println("Time to be converted to hh:mm:ss: ");
int time = input2.nextInt();
int hr = 0, min = 0, sec = 0;
if(time >= 3600)
{
hr = time/3600;
time = time%3600;
}
if(time >= 60)
{
time = time%3600;
min = time/60;
}
if (time < 60)
{
sec += time;
}
System.out.println(hr+":"+min+":"+sec);
So, my problem is that whenever the seconds is greater than 3719, the minutes and hours stop incrementing.
Example:
3719
1:1:59
3720
1:1:0
7325
1:1:0
Ive also tried a different algorithm, using subtraction instead of modulus
Scanner input2 = new Scanner(System.in);
System.out.println("Time to be converted to hh:mm:ss: ");
int time = input2.nextInt();
int hr = 0, min = 0, sec = 0;
if(time >= 3600)
{
hr ++;
time -=3600;
}
if(time >= 60)
{
time -= 60;
min ++;
}
if (time < 60)
{
sec += time;
}
System.out.println(hr+":"+min+":"+sec);
Upvotes: 0
Views: 554
Reputation: 338306
Duration.ofSeconds ( 7_325L ).toString()
PT2H2M5S
The Answer by DimaSan is correct, addressing the math issue.
FYI, the java.time classes can do this math work for you.
Also, tracking a span of time as a String in the format of hh:mm:ss
is not recommended. That format creates ambiguity as it looks like a time-of-day. The ISO 8601 standard defines many sensible unambiguous formats for date-time values. The one for a span of time not attached to the timeline is known as durations: PnYnMnDTnHnMnS
. The P
marks the beginning, the T
separates years-month-days from hours-minutes-seconds. For example, "P3Y6M4DT12H30M5S" represents a duration of "three years, six months, four days, twelve hours, thirty minutes, and five seconds". One and a half minutes is PT1M30S
.
The java.time classes use ISO 8601 formats by default when parsing/generating Strings.
java.time.Duration
The Duration
class in java.time represents a span of time as a total number of seconds and of a fractional second as nanoseconds.
Duration d = Duration.ofSeconds( 3_719L );
String output = d.toString();
PT1H1M59S
Next, a larger number.
Duration d = Duration.ofSeconds ( 7_325L );
PT2H2M5S
In Java 8 this class inexplicably lacks getter methods for the various parts such as the number of hours and the number of minutes and the number of seconds. Remedied in Java 9 with the addition of getPart
methods.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 1
Reputation: 896
Instead of:
if(time >= 60)
{
time = time%3600;
min = time/60;
}
there should be:
if(time >= 60)
{
min = time/60;
time = time%60;
}
You can also skip the if condition and add leading zeros:
public final static void main(String[] args) {
Scanner input2 = new Scanner(System.in);
System.out.println("Time to be converted to hh:mm:ss: ");
int time = input2.nextInt();
int hr = 0, min = 0, sec = 0;
hr = time/3600;
time = time%3600;
min = time/60;
sec = time%60;
System.out.println(String.format("%02d:%02d:%02d", hr, min, sec));
}
Upvotes: 1
Reputation: 5558
Your second algorithm would work, although it should use while
instead of if
so that it can increment multiple times.
Scanner input2 = new Scanner(System.in);
System.out.println("Time to be converted to hh:mm:ss: ");
int time = input2.nextInt();
int hr = 0, min = 0, sec = 0;
while(time >= 3600)
{
hr++;
time -= 3600;
}
while(time >= 60)
{
time -= 60;
min++;
}
while(time < 60)
{
sec += time;
}
System.out.println(hr+":"+min+":"+sec);
Upvotes: 1
Reputation: 44965
Let's avoid reinventing the wheel, just use SimpleDateFormat
with the appropriate pattern as next:
Scanner input2 = new Scanner(System.in);
System.out.println("Time to be converted to hh:mm:ss: ");
int time = input2.nextInt();
// Create the formater
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss");
// Multiply the time in sec by 1000 to get milliseconds
// Then convert the total amount of milliseconds into a Date
// And finally format the date
System.out.println(format.format(new Date(time * 1000L)));
Upvotes: 1
Reputation: 12684
That's because you are using 3600 in modulus for min
:
if (time >= 60) {
min = time / 60;
time = time % 3600;
}
but you must use 60, change it to:
public static void main(String[] args) {
Scanner input2 = new Scanner(System.in);
System.out.println("Time to be converted to hh:mm:ss: ");
int time = input2.nextInt();
int hr = 0, min = 0, sec = 0;
if (time >= 3600) {
hr = time / 3600;
time = time % 3600;
}
if (time >= 60) {
min = time / 60;
time = time % 60;
}
if (time < 60) {
sec += time;
}
System.out.println(hr + ":" + min + ":" + sec);
}
and it will work as you expect.
Check it here http://ideone.com/unTQTW
Upvotes: 2