aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Making seconds update Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Making seconds update" Watch "Making seconds update" New topic
Author

Making seconds update

Rodney Ibanez
Greenhorn

Joined: Aug 30, 2012
Posts: 17
Hi. I am really very eager to learn java. I've read many books about it and had learned the language on my own. Can you please help. I have created a jframe with date and time. The date is okay but the time in seconds is not updating every second. How can I make it update each second. Here is my code.

public void currentDate(){
Calendar calendar = new GregorianCalendar();
int month = calendar.get(calendar.MONTH);
int year = calendar.get(calendar.YEAR);
int day = calendar.get(calendar.DAY_OF_MONTH);
int second = calendar.get(calendar.SECOND);
int minute = calendar.get(calendar.MINUTE);
int hour = calendar.get(calendar.HOUR_OF_DAY);

lblDate.setText(month+1 +"/" +day +"/" +year);
lblTime.setText(hour +":" +minute +":" +second);
}
Thanks!
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1075
    
  10

Welcome to the Ranch!

Please UseCodeTags when posting code. You have posted only the part that dispays the date. If you want this to be updated every second, you should use a javax.swing.Timer. You can read the tutorial to find out how to use timers.

Also, you can make use of the SimpleDateFormat class to get the display in a simpler manner.

Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Hi,

please use code tag for your code. that way it is more convinient to read.

For your timer you can Timer class of swing package. Instead if you want to learn java, then use a Thread that notifies the time every second (try to implement this concept, you will cover some more topics while doing so)

Regards,
Vishal


Programming is about thinking, NOT coding
Rodney Ibanez
Greenhorn

Joined: Aug 30, 2012
Posts: 17
thank you. I used the timer you suggested and now my program is running fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making seconds update
 
Similar Threads
Java API - to get next year
Is java Date/Calendar a bug?
Java.util.Calendar
Calendar Object - updateTime()
What fields does java.util.Date.equals compare?