I have converted a 24 Hour Clock to the 12 hour clock. But for some reason when I put in 0:30 in it outputs it as 1:30am (Supposed to go to 12:30am) And when i put in 12:30 it goes to 1:30AM (Supposed to go to 12:30PM).
Can anyone help me with my problem?
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | /** * Write a description of class TwelveHourClock here. * * @author (your name) * @version (a version number or a date) */ public class TwelveHourClock { public NumberDisplay hours; public NumberDisplay minutes; public String displayString; private int timeZone = 1 ; public TwelveHourClock() { hours = new NumberDisplay( 12 ); minutes = new NumberDisplay( 60 ); setTime( 12 , 0 ); } public TwelveHourClock( int hour, int minute){ hours = new NumberDisplay( 12 ); minutes = new NumberDisplay( 60 ); setTime(hour, minute); } private void updateDisplay(){ if (timeZone == 1 ){ displayString = getHourDisplay()+ ":" +minutes.getDisplayString() + " AM" ; } else if (timeZone == 2 ){ displayString = getHourDisplay()+ ":" +minutes.getDisplayString() + " PM" ; } } public String getHourDisplay(){ int value = hours.getValue()+ 1 ; if (String.valueOf(value).length() == 1 ){ return "0" +value; } return value+ "" ; } public void timeTick(){ if (minutes.getValue() == 59 ){ hours.increment(); } minutes.increment(); updateDisplay(); } public void setTime( int hour, int minute){ hours.setValue(hour); minutes.setValue(minute); updateDisplay(); } public String getTime(){ return displayString; } } |