# Thread: 24 hour clock to 12 hour clock project.

1. Member
Join Date
Oct 2009
Posts
10
Rep Power
0

## 24 hour clock to 12 hour clock project.

Hi guys, i am working through a java book and have come across this challenge exercise where we are told to create a 24 hour clock which i have accomplished.

They now have told us to change the 24 hour clock into a 12 hour clock which i have 90% completed but am having a problem with the am/pm string that is shown at the end of the clock display.

Currently i have used if statements to do the following,

public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours = hours + 1;
}
if(hours > 12)
{
hours = hours -12;
}
updateDisplay();
}

This gets the value if its 13+ and subtracts 12 to get the 12hour clock result.

The problem i am having is the following to display the am/pm

I created the following method,

private String updateDisplayTwo()
{
if(hours > 12)
{
amPm = "pm";
}
else
{
amPm = "am";
}

return amPm;
}

Now i know where the problem lies as the hours data is never going to be 13+ anymore because if it is then 12 is subtracted from it.

What i was wondering is there anything within java that will 'show' when a method had been used and been activated. As i belive if i can tell when the 12> function is being used i can use that in another IF statement to show the pm string instead of the constant am (currently).

Thanks in advance guys.

2. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
7
Suppose you a boolean variable that can be accessed by both if statements. If this variable starts of with the value false, then you can use the first if to update it. The second if can then have a look at the same variable to see if the first if was used or not.

3. gcampton Guest
a extreeeeemely long if /else if would solve this.

if (hours == 0) time is 12am
else if (hours between 1-11) time is am
else if (hours == 12) time is pm
else if (hours between 13-23) time is pm and minus 12.
else if (hours == 24) time is 12am

in that order you won't get repeat assignments.
remember army time both 0hundred hours and 24hundred hours is 12am, usually 0hundred is referred to as past tense, and 24hundred hours pre-tense.
But most people disregard standard meanings and mix them up anyway... you can have 0:32:14 and 24:32:14 both are 12:32:14 am

http://en.wikipedia.org/wiki/24-hour_clock

Time-of-day notations beyond 24:00 (such as 24:01 or 25:59) are neither commonly used nor covered by any relevant standards. However, they have been observed occasionally in some special contexts in Japan and Hong Kong where business hours extend beyond midnight, such as broadcast-television production and with time-validation stamps on some European public transport systems, such as those used in Copenhagen, which may show, for example, 27:45 instead of 03:45.

Most digital clocks and watches show times between 00:00 to 23:59. As a notable exception, the digital clocks of at least one European manufacturer of kitchen appliances show 24:00 for midnight, rather than the standard 00:00, with an example being Bosch Siemens microwaves.
if you are doing this for assignment or for other to use then you should inlcude, at least up to 24:00:00 as some people are easily confused.
Last edited by gcampton; 11-06-2009 at 05:43 PM.

4. Member
Join Date
Jan 2013
Posts
1
Rep Power
0

## Re: 24 hour clock to 12 hour clock project.

Hi, i am doing the same assignment for my uni this year please can i ask you for a help if you have done this assignment already can i get the proper code as i am stuck in various places please if you can mail me the actual coding on my email address which is [email address removed] i'll really appreciate that

my assignment is due tomorrow please try an reply me back ASAP!
Last edited by DarrylBurke; 01-08-2013 at 12:48 PM. Reason: Removed email address

5. ## Re: 24 hour clock to 12 hour clock project.

Don't resurrect a very old thread; start your own thread and don't beg for code; that's cheating.

kind regards,

Jos

ps. I'm closing this threaad.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•