Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making seconds update

 
Rodney Ibanez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rodney Ibanez
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you. I used the timer you suggested and now my program is running fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic