Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:21:26+00:00 2026-06-17T22:21:26+00:00

Okay so I have a clock class which is initially set as a 24

  • 0

Okay so I have a clock class which is initially set as a 24 hour clock but I have to have the clock display as a 12 hour clock using only this method! I have tried for hours but have fallen short! Please help. Make the clock display the time as a 12 hour clock (hh:mm:ss AM/PM). Use abstraction – don’t change anything but the updateDisplay() method. The clock can use military time “under the covers”; it just needs to return the proper string when getTime() is called.

private void updateDisplay()
{
    String AM = "AM";
    String PM = "PM";

    if(hours.getValue() == 0) {
        hours.setValue(hours.getValue() + 12);
        PM = "";
    }
    else if(hours.getValue() == 1 < hours.getValue() && hours.getValue() < 11) {
        PM = "";
    }
    else if(hours.getValue() == 12) {
       AM = "";
    }
    else if(hours.getValue() == 13 > 24) {
        hours.setValue(hours.getValue() - 12);
       AM = "";
    }

    displayString = hours.getDisplayValue() + ":" + 
    minutes.getDisplayValue() + ":" +
    seconds.getDisplayValue() + PM + AM;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:21:27+00:00Added an answer on June 17, 2026 at 10:21 pm

    A 24 Hour clock, H, runs from 0<=H<=23. If H div 12 is 0, then AM, else PM. Then the 12 Hour clock, h, is H mod 12 unless that value is 0, in which case h = 12.
    You shouldn’t be changing the value (hours.setValue(...)) in your display code, nor do you need to convert to seconds.
    I think the issue with your code is just a syntax problem in the first conditional statement.

    ... if(hours.getValue() == 1 < hours.getValue() ... // This is wrong
    

    You are comparing the value of hours to 1, and then the result of the comparison to hours. This won’t compile in java, because bools are incomparable to ints.

    Note: When I say div, I mean an integer division (i.e. truncated / operator in Java), and when I say mod I mean an integer modulus (% operator in Java).

    Update: All you need to do is convert your 24 hour value hours.getValue() to the 12 Hour equivalent for display. Something along the lines of:

    // Not correct Java!
    string meridianNess = (hours.getValue()/12==0?"AM":"PM");
    int h = hours.getValue()%12;
    if (h==0) h = 12;
    System.out.print(h+":"+new NumberFormat("00").format(minutes.getValue())+":"+
        new NumberFormat("00").format(seconds.getValue())+" "+meridianNess);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I have a series of objects based on a base class which are
okay? I have a button that is inside an updatepanel, click on this button
okay i have been trying to understand this for hours i am learning VB
Okay I have updated my code a little, but I am still not exactly
Okay - an easy answer for 99% of you. I have a custom class
Okay I've been trying to research how to do this and have failed. I
I am practising c# with a phonebook example using Hashtable. I have a class
Okay, I have an issue using Knockout in that it isn't able to parse
i have a simple slideshow which is working okay so far. it consists of
I have this fsField is the class of all elements in the form. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.