advancing 60 min to hour and 2 digit display

Hello everyone, I have a question and need some help with this. I know i need to create an If statement to look at the hours and minutes as well as het the am/pm working. I have a idea about how it should be done but am not clear how to approach it. How do we handle the time display to be 2 digits 00 I have included the current code and need some up with this part.

amPmDisplay.setText("am");

If (hr > 12){
ampmDisplay.setText("pm");
hr = 1   // do we handle the time display to be 2 digits 00

}
If (min > 59{
hr = hr + 1   //do we handle the time display to be 2 digits 00
min = 0 //do we handle the time display to be 2 digits 00
}




                    if (selected == HR)
                        {
                            String time = guiPanel.timeDisplay.getText();
                            String[] splitTime = time.split(":");

                            int hr = Integer.parseInt(splitTime[0].trim());
                            time = Integer.toString(hr + 1)  + ":" + splitTime[1] + ":" + splitTime[2];

                            guiPanel.timeDisplay.setText(time); //modified time
                        }
                        else                            if (selected == MIN)
                            {
                                String time = guiPanel.timeDisplay.getText();
                                String[] splitTime = time.split(":");

                                int min = Integer.parseInt(splitTime[1].trim());
                                time = splitTime[0] + ":" + Integer.toString(min + 1)  + ":" + splitTime[2];

                                guiPanel.timeDisplay.setText(time); //modified time
                            }
                    }
                    else                        if (cmd.equals(guiPanel.downButton.getText()))
                        {
                            if (selected == HR)
                            {
                                String time = guiPanel.timeDisplay.getText();
                                String[] splitTime = time.split(":");

                                int hr = Integer.parseInt(splitTime[0].trim());
                                time = Integer.toString(hr - 1)  + ":" + splitTime[1] + ":" + splitTime[2];

                                guiPanel.timeDisplay.setText(time); //modified time
                            }
                            else                                if (selected == MIN)
                                {
                                    String time = guiPanel.timeDisplay.getText();
                                    String[] splitTime = time.split(":");

                                    int min = Integer.parseInt(splitTime[1].trim());
                                    time = splitTime[0] + ":" + Integer.toString(min - 1)  + ":" + splitTime[2];

                                    guiPanel.timeDisplay.setText(time); //modified time
                                }
                        }
Drop_of_RainAsked:
Who is Participating?
 
objectsCommented:
If you use a DateFormat as I showed you in one of your previous questions you don't need to do any of that. It will handle all the formatting for you.
0
 
Drop_of_RainAuthor Commented:
I know that but it is the adjusting part that I don't see how to do. I haven't seen any examples except from converting from milliseconds. Is that what I am going to have to do?
0
 
objectsCommented:
>                             String time = guiPanel.timeDisplay.getText();

And I also explained the advantages of storing the current time displayed as a Calendar. Saving you having to parse the string everytime you want the value

eg. to add an hour

Calendar time = guiPanel.timeDisplay.getTime();
time.add(Calendar.HOUR, 1);
guiPanel.timeDisplay.update();


where the update() just does

label.setText(dateFormat.format(currentTime));

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Drop_of_RainAuthor Commented:
So I would use that with an actionListener of the up and down buttons?  Could you give me an example please. This is a much easier way, I have tried to get this in other questions but it keep getting missed. This is much easier. I will need to convert all of the time at the end to milliseconds. These are int? How would i do that with this time thing?
0
 
objectsCommented:
Yes, you would put it in your listener

public void TimeAdjuster impleemnts ActionListener
{
   private TimeDisplay timeDisplay;
   private int field;
   private int delta;

   public TimeAdjuster(TimeDisplay timeDisplay, int field, int delta)
   {
       this.timeDisplay = timeDisplay;
       this.field = field;
       this.delta - delta;
   }

   public void actionPerformed(ActionEvent e)
   {
      Calendar time = timeDisplay.getTime();   // get current time displayed
      time.add(field, delta);                              // adjust the time
      timeDisplay.update();                              // update display of time
   }
}


Then to use it you would create a instance for each button

eg. for minute  buttons:

minuteUpButton.addActionListener(new TimeAdjuster(guiPanel.timeDisplay, Calendar.MINUTE, 1));
minuteDownButton.addActionListener(new TimeAdjuster(guiPanel.timeDisplay, Calendar.MINUTE, -1));


(notice not one if/switch statement ;)
0
 
ashwanilabsCommented:
OR You can use this function to make your hrs,mins and sec in 2 digits by passing fiLen as 2

public static String zeroFill (String fsInString,int fiLen)
{
String lsOutString = new String(fsInString.trim());
while (lsOutString.length() < fiLen)
{
    lsOutString = "0" + lsOutString;
}
return lsOutString;
}

and call it as follows from your code:-
time = zeroFill(Integer.toString(hr + 1),2)  + ":" + zeroFill(splitTime[1],2) + ":" + zeroFill(splitTime[2],2);
0
 
objectsCommented:
> public void TimeAdjuster impleemnts ActionListener

typo, should be:

public class TimeAdjuster implements ActionListener
0
 
objectsCommented:
> I will need to convert all of the time at the end to milliseconds.

long millisecs = time.getTime().getTime();
0
 
Drop_of_RainAuthor Commented:
I do have to say objects code is very readable and simple. I don't know much but i can see that. I wondered if ther was something like this just didn't know how to say or ask it. It seems to easy.

Please explain this part i get evrything else.
this.field = field;
this.delta - delta;
 what is the field and the delta. I know they are int, but I don't see their usage in the code so I can understand them.

0
 
Drop_of_RainAuthor Commented:
I want to be clear what i am doing in this class. I am starting with the current time call from calender. I am making thet time adjustable so the user can set an alarm. I will need to convert the adjusted time to milliseconds for a timer. It lloks like the code above is for getting the milliseconds of the total current time is this correct?
0
 
objectsCommented:
field - is the part of time that the action should update. This allows the same class to be used to update hours, minutes and seconds. The Calendar class contains a set of constants for these fields.

delta - is the amount each click should adjust the field, in your case it would be +1 and -1 to increaes/decrease the time. Again allowing the same class to be reused to increase and decrease (as shown in the code above).

> It lloks like the code above is for getting the milliseconds of the total current time is this correct?

correct, you'd just need to offset it for a timer (ie. subtract the time at 0:00:00)
0
 
Drop_of_RainAuthor Commented:
Thanks that was very helpful, just one more thing. The offset thing. Is that the comparing of the two and then converting them. I think i need a little help her still.
0
 
objectsCommented:
take the # millisecs at time 0:00, and subtract that from millis at the stopwatch time.
That'll give u the number of millis elapsed.
To implement it you could just store the time at 0:00.
0
 
Drop_of_RainAuthor Commented:
How will i tie the 2 buttons together, the hour, minute, up, down are all seperate buttons?

minuteUpButton.addActionListener(new TimeAdjuster(guiPanel.timeDisplay, Calendar.MINUTE, 1));
minuteDownButton.addActionListener(new TimeAdjuster(guiPanel.timeDisplay, Calendar.MINUTE, -1));
0
 
objectsCommented:
they are seperate buttons, how you lay them out in your gui makes no difference to how they are wired.
you'd probably want to group the up and down i'd think.
0
 
Drop_of_RainAuthor Commented:
I can use an actionCommand which will give me the buttons text in an If statement for hours or minutes  in the up and down buttons action events correct or what do you suggest?
0
 
objectsCommented:
no, you don't need to worry about actionCommands. The above listener will handle updating the time correctly without need for any if/.switch statements.
Each button will have a different listener that performs the correct behaviour for that button.
0
 
Drop_of_RainAuthor Commented:
Hi objects you miss understood what i meant. What I see in the code above is that it is for the up and down button, the code is changing the minute time. The way the GUI is set up is the hour button is pressed just like a wristwatch and then the up and down buttons are pressed, it works the same way for the minutes. How would that get coded using listeners without some kind of checking.
0
 
Drop_of_RainAuthor Commented:
Here is a simple way I can see of doing it.

   public void actionPerformed(ActionEvent e)
    {
        String cmd = e.getActionCommand();
       
        if (cmd.equals(this.guiPanel.hourButton))
        {
            upButton.addActionListener(new TimeAdjuster(guiPanel.timeDisplay, Calendar.HOUR, 1));
           downButton.addActionListener(new TimeAdjuster(guiPanel.timeDisplay, Calendar.HOUR, -1));
          }
            if (cmd.equals(this.guiPanel.minutesButton))
            {
               upButton.addActionListener(new TimeAdjuster(guiPanel.timeDisplay, Calendar.MINUTE, 1));
               downButton.addActionListener(new TimeAdjuster(guiPanel.timeDisplay, Calendar.MINUTE, -1));

            }
      }  
0
 
Drop_of_RainAuthor Commented:
Would this be correct for the date as well?


public void DateAdjuster implements ActionListener
{
   private dateDisplay timeDisplay;
   private int field;
   private int delta;

   public TimeAdjuster(dateDisplay timeDisplay, int field, int delta)
   {
       this.timeDisplay = timeDisplay;
       this.field = field;
       this.delta - delta;
   }

   public void actionPerformed(ActionEvent e)
   {
      Calendar day = timeDisplay.getDate();   // get current day displayed
      Calendar month = timeDisplay.getMonth();   // get current month displayed
      Calendar year = timeDisplay.getYear();   // get current year displayed
      day.add(field, delta);                              // adjust the day
      dateDisplay.update();                              // update display of day
      month.add(field, delta);                              // adjust the month
      dateDisplay.update();                              // update display of month
      year.add(field, delta);                              // adjust the year
      dateDisplay.update();                              // update display of year

}
0
 
objectsCommented:
no, thaqts what the field is for

hourUpButton.addActionListener(new TimeAdjuster(guiPanel.timeDisplay, Calendar.HOUR, 1));
hourDownButton.addActionListener(new TimeAdjuster(guiPanel.timeDisplay, Calendar.HOUR, -1));
0
 
Drop_of_RainAuthor Commented:
There are only 2 buttons that are up and down, there are not separate ones for hours and minutes. The field has nothing to do with the GUI wiring from what i can tell reading Sun's info on it.. The user chooses beween the hour and minute by pressing the button. How can the same button be able to do two different things one for the hours and the other for minutes?
I had already created the code you posted in your last post.
I have another question up requarding the blinking of the fields when they are active to be changed, just like a wristwatch., I have the basic code but am not sure how to tie it in with this calender thing.
0
 
objectsCommented:
ahh, in that case the button that switches between hours/minutes would change the value of field in the listener (or it could switch the listener).
Another option would be to have the listener access timeDisplay to determine the field (which I think I prefer).

public void TimeAdjuster impleemnts ActionListener
{
   private TimeDisplay timeDisplay;
   private int delta;

   public TimeAdjuster(TimeDisplay timeDisplay, int field, int delta)
   {
       this.timeDisplay = timeDisplay;
       this.delta = delta;
   }

   public void actionPerformed(ActionEvent e)
   {
      Calendar time = timeDisplay.getTime();   // get current time displayed
      time.add(timeDisplay.getField(), delta);                              // adjust the time
      timeDisplay.update();                              // update display of time
   }
}

0
 
Drop_of_RainAuthor Commented:
<the button that switches between hours/minutes would change the value of field in the listener
! button for hours and 1 for minutes.

Let me ask you a question, I have seen tons of examples in code where if ststements checks which button has been presses. I could see some concern if there was to many, but in this case there is only 2 button in question the other have their own job to do. Please explain to me why you are so against using them? I want to understand your reasoning.
0
 
objectsCommented:
because they are unnecessary in your case (and many examples on the web).
The web is full of as many (porobably more) bad examples as there are good.

Your listener (and classes in general) should perform simple, well defined tasks. In your case your listener updates a field, and it shouldn't care about what field it is updating.
Imagone you decide to add the ability to update the day as well as hour and minute.if using if/switch statement you would need to modify your action class to adding another if/case further complicating your code. With what I suggested you would not need to make any change to the class at all. Would just need a change to the action that switches mode to specify the field to update, which makes more sense as it knows and controls which field to update.
Also if you change how a field needs to be updated, you would need to repeat that change in *every* if/case, being error prone.
0
 
Drop_of_RainAuthor Commented:
That makes lots of sense, this is really what object programing is meant to accomplish. The ability to upgrade without have to impact what has already been done. I am not pushing for any sollution, I want to understand why the suggestion are given from a larger picture. Yiou explained it very well, and i can see that this is the right choice and direction to follow.
I changed the listeners on the hour, minute, up and down buttons to hourEvent ,minuteEvent upEvent and downEvent that way it makes the code readable. I have seen so many examples that all they use is a (e) but they are only using one to watch many buttons. I like this approach.
But I am still lost a little with how the hour and minutes buttons work in your code.
0
 
Drop_of_RainAuthor Commented:
Why is TimeDisplay Capitalized it is a JLabel named timeDisplay, which is gone now. I have changed to 4 labels hoursDisplay, minutesDisplay, secondsDisplay, amPmDisplay. Am I going to have to write one of these for each label?

public void TimeAdjuster impleemnts ActionListener
{
   private TimeDisplay timeDisplay;
   private int delta;

   public TimeAdjuster(TimeDisplay timeDisplay, int field, int delta)
   {
       this.timeDisplay = timeDisplay;
       this.delta = delta;
   }

   public void actionPerformed(ActionEvent e)
   {
      Calendar time = timeDisplay.getTime();   // get current time displayed
      time.add(timeDisplay.getField(), delta);                              // adjust the time
      timeDisplay.update();                              // update display of time
   }
}

Is this correct?

public void HourAdjuster impleemnts ActionListener
{
   private HourDisplay hourDisplay;
   private int delta;

   public TimeAdjuster(HourDisplay hourDisplay, int field, int delta)
   {
       this.hourDisplay = hourDisplay;
       this.delta = delta;
   }

   public void actionPerformed(ActionEvent e)
   {
      Calendar hour = hourDisplay.getHour();   // get current time displayed
      hour.add(hourDisplay.getField(), delta);                              // adjust the time
      hourDisplay.update();                              // update display of time
   }
}
0
 
objectsCommented:
TimeDisplay is the class that manages the display of a field.
You can use the same class to manage all fields, ie. you don't need a seperate class for hours and minutes.
0
 
Drop_of_RainAuthor Commented:
Ok I thought it was the JLabel because it is spelled the same way. So in just include all of the display info in it?  Another thing in this line of code:
   public HourAdjuster(HourDisplay hoursDisplay, int field, int delta)  

What is the HourDisplay?
0
 
Drop_of_RainAuthor Commented:
Forget the last part that is my code. I am going to need a little direction with this OK. So will the TimeDisplay be passed hourDisplay which will didplay the hours?
0
 
Drop_of_RainAuthor Commented:
Will this do?

public void TimeAdjuster impleemnts ActionListener
{
   private HourDisplay hoursDisplay;
   private int delta;

   public TimeAdjuster(TimeDisplay timeDisplay, int field, int delta)
   {
       this.timeDisplay = timeDisplay;
       this.delta = delta;
   }

   public void actionPerformed(ActionEvent e)
   {
      Calendar hour = hoursDisplay.getHour();     // get current hour displayed
      hour.add(hoursDisplay.getField(), delta);   // adjust the hour
      hoursDisplay.update();                      // update display of hour
      Calendar minutes = minutesDisplay.getMinute();   // get current minutes displayed
      hour.add(hourDisplay.getField(), delta);         // adjust the minutes
      minutesDisplay.update();                         // update display of minutes  
   }
}
0
 
Drop_of_RainAuthor Commented:
>  private HourDisplay hoursDisplay;
  > private int delta;

It should be this

   private TimeDisplay timeDisplay;
   private int delta;
0
 
objectsCommented:
no, the action should only upgrade one field.
you'll create two *instances* of that class, one to handle the hour and the other to handle minute
0
 
Drop_of_RainAuthor Commented:
Can you show me?
0
 
objectsCommented:
hang on, lost track of where we were at
TimeDisplay manages the display of time (hour and minute)
The getField() method returns the current field (ie. Calendar.HOUR or Calendar.MINUTE)

public void TimeAdjuster impleemnts ActionListener
{
   private TimeDisplay timeDisplay;
   private int delta;

   public TimeAdjuster(TimeDisplay timeDisplay, int delta)
   {
       this.timeDisplay = timeDisplay;
       this.delta = delta;
   }

   public void actionPerformed(ActionEvent e)
   {
      Calendar time = timeDisplay.getTime();   // get current time displayed
      time.add(timeDisplay.getField(), delta);                              // adjust the time
      timeDisplay.update();                              // update display of time
   }
}
0
 
Drop_of_RainAuthor Commented:
Ok so it works with the current field. Then how will I wire this actionlistener with the hour button which would then make the hour the current field.?
0
 
objectsCommented:
> Then how will I wire this actionlistener with the hour button which would then make the hour the current field.?

I thought u didn't have a specific hour button, and instead just a + and - button, and another to switch between hour and miniute
0
 
Drop_of_RainAuthor Commented:
No I have set this up just like a wristwatch. A mode button, a set button, hour button, minute button,
up button, and down Button. It is to work like this. The mode is cycled to ALARM and the OK button is pressed. Then some buttons are disabled, the record, play and stop buttons. They press the hour button, the hour display start blinking just like a wristwatch does. The up and down buttons move the numbers up and down. When the time is reached the OK button is pressed. This is the same for the timer except the display is changed to 00:00:00 and the down buttons are disabled while the numbers are 0. The user mover the amount of time in hours and minutes until they get the a,mount of time they want, then they press the OK button. This is a basic operation of the program time aspects. I hope this helps you .
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.