im designing a 12 hour clock, converting it from a 24 hour clock. i had the idea of keeping the clock internally as 24 hours, and externally showing it as a 12 hour clock. The code I have is

public String getTime()  
{
    if (hours.getValue() >12)
    {
       hours.setValue(hours.getValue() - 12);
       System.out.println(hours.getDisplayValue() + ":"+ minutes.getDisplayValue() +"pm");
    }

    else
    {
        System.out.println (hours.getDisplayValue() + ":"+ minutes.getDisplayValue() + "am");
    }
    if(hours.getValue() == 0)
    {
        System.out.println("12"+":"+ minutes.getDisplayValue() + "am");
    }
    return hours.getDisplayValue() + ":"+ minutes.getDisplayValue();
}

It works fine until I use the function of tick which increases the minutes by 1. because of the maths i have in the code hours - 12 the hours become under 12, and so it prints the time after as AM. So for example I create the time 13:00 it shows as 1:00pm then i use the tick method it changes to 1:01 am. I would be extremely grateful if someone could help me sort out my code. Thank you for your time

my whole code

public class ClockDisplay
{
    private NumberDisplay hours;
    private NumberDisplay minutes;
    private String displayString;    // simulates the actual display
    private int alarmState;


    /**
     * Constructor for ClockDisplay objects. This constructor 
     * creates a new clock set at 12:00am.
     */
    public ClockDisplay()
    {
        hours = new NumberDisplay(24);
        minutes = new NumberDisplay(60);
        updateDisplay();
        alarmState = 0;
    }

    /**
     * Constructor for ClockDisplay objects. This constructor
     * creates a new clock set at the time specified by the 
     * parameters.
     */
    public ClockDisplay(int hour, int minute)
    {
        hours = new NumberDisplay(24);
        minutes = new NumberDisplay(60);
        setTime(hour, minute);


    }

    /**
     * This method should get called once every minute - it makes
     * the clock display go one minute forward.
     */
    public void timeTick()
    {
        minutes.increment();
        if(minutes.getValue() == 0) {  // it just rolled over!
            hours.increment();

        }
        updateDisplay();
    }

    /**
     * Set the time of the display to the specified hour and
     * minute.
     */
    public void setTime(int hour, int minute)
    {
        hours.setValue(hour);
        minutes.setValue(minute);
        updateDisplay();
    }

    /**
     * Return the current time of this display in the format HH:MM.
     */
    public String getTime()  
    {
        if (hours.getValue() >12)
        {
           hours.setValue(hours.getValue() - 12);
           System.out.println(hours.getDisplayValue() + ":"+ minutes.getDisplayValue() +"pm");
        }

        else
        {
            System.out.println (hours.getDisplayValue() + ":"+ minutes.getDisplayValue() + "am");
        }
        if(hours.getValue() == 0)
        {
            System.out.println("12"+":"+ minutes.getDisplayValue() + "am");
        }
        return hours.getDisplayValue() + ":"+ minutes.getDisplayValue();
    }

    /**
     * Update the internal string that represents the display.
     */
    private void updateDisplay()
    {
        displayString = hours.getDisplayValue() + ":" + 
                        minutes.getDisplayValue();
    }

Just drop the call to set hours to be less than twelve

hours.setValue(hours.getValue() - 12);

and leave it in 24-hour time. You can display it in 12-hour format without altering the underlying value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.