title: Creating a Time class in Java
body: <p>So my task is to create a Time class that holds an hour value and a minute value to represent a time.  I'll be using "military time", so 12:01 am is 0001 and 1 pm is 1300.  I may assume valid military times range from 0000 to 2359. Valid standard times range from 12:00 am to 11:59 pm.  Time should include two constructors:</p>
  
  <p>Time() - Default constructor that sets the time to 1200.
  Time(int h, int m) - If h is between 1 and 23 inclusive, set the hour to h. Otherwise, set the hour to 0. If m is between 0 and 59 inclusive, set the minutes to m. Otherwise, set the minutes to 0.
  Time should include the following methods:</p>
  
  <p>String toString() - Returns the time as a String of length 4 in the format: 0819. Notice that if the hour or minute is one digit, it should print a zero first. For example, 6 should print as 06.</p>
  
  <p>String convert() - Returns the time as a String converted from military time to standard time. For example, 0545 becomes 5:45 am and 1306 becomes 1:06 pm.</p>
  
  <p>void increment() - Advances the time by one minute. Remember that 60 minutes = 1 hour. Therefore, if your time was 0359, and you add one minute, it becomes 0400. 2359 should increment to 0000.</p>
  
  <p>This is what I've written (with the exception of the main method):</p>
  
  <pre><code>import java.io.IOException;
  
  public class Time
  {
  
      private int hour;
      private int minute;
      public Time()
      {
          hour = 12;
          minute = 0;
      }
  
  
      public Time(int h, int m)
      {
          if ( h &gt;= 1 &amp;&amp; h &lt;= 23)
              h = hour;
          else 
              hour = 0;
          if ( m &gt;= 0 &amp;&amp; m &lt;= 59)
              m = minute;
          else 
              minute = 0;
      }
  
      public String toString()
      {
          String s = "";
          if ( hour &lt; 10 &amp;&amp; minute &lt; 10)
              s = "0" + hour + "0" + minute;
          else if ( hour &lt; 10 &amp;&amp; minute &gt; 10)
              s = "0" + hour + minute;
          else if ( hour &gt; 10 &amp;&amp; minute &lt; 10)
              s = hour + "0" + minute;
          else if ( hour == 0)
              s = "0" + hour + minute;
          else if ( minute == 0)
              s = hour + "0" + minute;
  
          return s;
  
      }
  
      public String convert()
      {
          String c = "";
          if ( hour &gt; 12)
              c = "0" + (24 - hour) + minute + "pm";
          else
              c = hour + minute + "am";
          return c;
      }
  
      public void increment()
      {
          minute++;
          if (minute == 60)
          {
              hour++;
              minute = 0;
          }
          else if ( hour == 24)
              hour = 0;
  
      }
  
       public static void main(String str[]) throws IOException
       {
  
        Time time1 = new Time(14, 56);
        System.out.println("time1: " + time1);
        System.out.println("convert time1 to standard time: " + time1.convert());
        System.out.println("time1: " + time1);
        System.out.print("increment time1 five times: ");
        time1.increment();
        time1.increment();
        time1.increment();
        time1.increment();
        time1.increment();
        System.out.println(time1 + "\n");
  
        Time time2 = new Time(-7, 12);
        System.out.println("time2: " + time2);
        System.out.print("increment time2 67 times: ");
        for (int i = 0; i &lt; 67; i++)
         time2.increment();
        System.out.println(time2);
        System.out.println("convert to time2 standard time: " + time2.convert());
        System.out.println("time2: " + time2 + "\n");
  
  
        Time time3 = new Time(5, 17);
        System.out.println("time3: " + time3);
        System.out.print("convert time3: ");
        System.out.println(time3.convert());
  
        Time time4 = new Time(12, 15);
        System.out.println("\ntime4: " + time4);
        System.out.println("convert time4: " + time4.convert());
  
        Time time5 = new Time(0, 15);
        System.out.println("\ntime5: " + time5);
        System.out.println("convert time5: " + time5.convert());
  
        Time time6 = new Time(24, 15);
        System.out.println("\ntime6: " + time6);
        System.out.println("convert time6: " + time6.convert());
  
        Time time7 = new Time(23,59);
        System.out.println("\ntime7: " + time7);
        System.out.println("convert time7: " + time7.convert());
        time7.increment();
        System.out.println("increment time7: " + time7);
        System.out.println("convert time7: " + time7.convert());
  
       }
  
  }
  </code></pre>
  
  <p>The output should be as follows:</p>
  
  <pre><code>time1: 1456
  convert time1 to standard time: 2:56 pm
  time1: 1456
  increment time1 five times: 1501
  
  time2: 0012
  increment time2 67 times: 0119
  convert to time2 standard time: 1:19 am
  time2: 0119
  
  time3: 0517
  convert time3: 5:17 am
  
  time4: 1215
  convert time4: 12:15 pm
  
  time5: 0015
  convert time5: 12:15 am
  
  time6: 0015
  convert time6: 12:15 am
  
  time7: 2359
  convert time7: 11:59 pm
  increment time7: 0000
  convert time7: 12:00 am
  </code></pre>
  
  <p>My current output is:  </p>
  
  <pre><code>time1: 0000
  convert time1 to standard time: 0am
  time1: 0000
  increment time1 five times: 0005
  
  time2: 0000
  increment time2 67 times: 0107
  convert to time2 standard time: 8am
  time2: 0107
  
  time3: 0000
  convert time3: 0am
  
  time4: 0000
  convert time4: 0am
  
  time5: 0000
  convert time5: 0am
  
  time6: 0000
  convert time6: 0am
  
  time7: 0000
  convert time7: 0am
  increment time7: 0001
  convert time7: 1am
  </code></pre>
  
  <p>My compiler is telling me that there are issues all over and I can't figure out where.  If somebody could please take a look and help me figure out what's wrong, it'd be very helpful.  Thank You!</p>