File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Making seconds update

Rodney Ibanez

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);
Ranganathan Kaliyur Mannar

Joined: Oct 16, 2003
Posts: 1101

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.

Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179

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)


Programming is about thinking, NOT coding
Rodney Ibanez

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:
subject: Making seconds update
jQuery in Action, 3rd edition