Note that there are some explanatory texts on larger screens.

plurals
  1. POPrinting out time in HH:MM format
    text
    copied!<p>I am writing a Java program where I need to increase the hour and minute by 1 and output the current time in HH:MM format. If the hour is 23 it should go back to 0 or the minute is 59 the minutes should go to 0 and the hours should go to the next hour. This is what i have so far, however I am not entirely sure it is right. I am also unsure if that is the proper way to make the time appear with a : between hours and minutes. Here is my code:</p> <pre><code>//TimeOfDay program public class TimeOfDay { private int hour; private int minute; /** * Constructor for objects of class TimeOfDay */ public TimeOfDay( int h, int m ) { setHr( h ); setMin( m ); String time = "17:46"; String [] values = time.split(":"); } public TimeOfDay() { } /** - set the hour using an input parameter h - set the minute using an input parameter m - add one hour (increase hour by 1) - add one minute (increase minute by 1) - output the current time in HH:MM format (single digit hour or minute must have a leading 0, e.g. 04:09) */ public void setHr (int h ) { if (0 &lt;= h &amp;&amp; h &lt; 23 ) hour = h; else hour = 0; } public void setMin (int m) { if (0 &lt;= m &amp;&amp; m &lt; 59) minute = m; else minute = 0; } public void addHour() { hour++; if ( hour &gt; 23 ) hour = 0; } public void addMinute() { minute++; if ( minute &gt; 59 ) { minute = 0; addHour(); //increment hour } } public int printCurrentTime() { if (hour &lt; 10) System.out.print( "0" + hour + ":"); if (minute &lt; 10) System.out.print( "0" + minute); return time.split; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload