0

This is the out put:

The time is 8:3:8AM The time is 8:3:8

This is the desired output:

The time is 08:03:08 AM The time is 08:03:08

So i need to convert this into the desired output where if it's 8:03:04 where 8 hours and 3 minutes and 4 seconds should be displyed like above. with zeros infront of the single digits

  package Assignment2;


  class clock{

  public static void main(String[] args) {
   clock timer = new clock();
   timer.setTime(8,02,59);
    timer.incTime(9);
     //timer.incTime(60);
       timer.display12hr();
     timer.display24hr();



           }


private int minutes, hours, seconds;

clock()
{
    minutes = 0;

    hours = 0;

    seconds = 0;

}
clock(int x, int y, int z)

{

    hours = x;

    minutes = y;

    seconds = z;
}

void setTime(int x, int y, int z)
{
    hours = x;
    minutes = y;
    seconds = z;
}

void incTime(int y)

{

    if (seconds + y > 59)

    {

        seconds = (seconds + y) - 60;
        minutes++;
    }
    else
        seconds += y;

}

void display12hr()

{
    if (hours > 12)
    {
        System.out.println("The time is " + (hours - 12) + ":" + minutes + ":" + seconds
                + "PM");
    }

    else
    {
        System.out.println("The time is " + (hours) + ":" + minutes + ":" + seconds +"AM");
    }
}

void display24hr()
{
    System.out.println("The time is " + (hours) + ":" + minutes + ":" + seconds);
}

}

john smith
  • 23
  • 1
  • 7

4 Answers4

2

Try using formatted output:

System.out.printf("%02d:%02d:%02d", hours, minutes, seconds);

This will pad numbers with a 0 when they don't have 2 digits, as described here.

Mauren
  • 1,955
  • 2
  • 18
  • 28
0

For any number you want to print, if it is < 10, you have to add a 0 in the front (assuming all values are printed to 2 places). Sounds like a job for a function...

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

You could use the DecimalFormat class.

Example:

int[] numbers = { 1, 5, 10, 15};
for(int num : numbers) {
    DecimalFormat formatter = new DecimalFormat("00");
    System.out.println(formatter.format(num));
}

Output:

01
05
10
15
0

Using the Calendar and SimpleDateFormat objects provides you with a rich date and time capability and rich date and time formatting capability respectively.

You can manually control the measurement of time similar to your class ....

    String datePattern = "hh:mm:ss";
    SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
    Calendar cal = Calendar.getInstance(Locale.US);
    System.out.println("Time = " + formatter.format(cal.getTimeInMillis()));
    cal.clear();
    cal.set(2013, 8, 6, 0, 0, 0);
    System.out.println("Start Time = " + formatter.format(cal.getTimeInMillis()));
    cal.set(2013, 8, 6, 8, 2, 59);
    System.out.println("Start Time = " + formatter.format(cal.getTimeInMillis()));
    cal.add(Calendar.SECOND, 9);
    System.out.println("End Time 12 hours = " + formatter.format(cal.getTimeInMillis()));
    datePattern = "HH:mm:ss";
    formatter = new SimpleDateFormat(datePattern);
    cal.add(Calendar.HOUR, 8);
    System.out.println("End Time 24 hours = " + formatter.format(cal.getTimeInMillis()));

resulting output:

    Time = 12:32:31
    Start Time = 12:00:00
    Start Time = 08:02:59
    End Time 12 hours = 08:03:08
    End Time 24 hours = 16:03:08

Or if you simply want start and stop times associated with events....

    datePattern = "yyyy-mm-dd hh:mm:ss:SS";
    formatter = new SimpleDateFormat(datePattern);
    System.out.println("start = " + formatter.format(Calendar.getInstance(Locale.US).getTimeInMillis()));
    System.out.println("end = " + formatter.format(Calendar.getInstance(Locale.US).getTimeInMillis()));

resulting output:

    start = 2013-32-07 12:32:31:656
    end = 2013-32-07 12:32:31:656

It depends on your objective but you have lots of options.

Threadid
  • 730
  • 1
  • 7
  • 27