• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Making seconds update

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you. I used the timer you suggested and now my program is running fine.
 
reply
    Bookmark Topic Watch Topic
  • New Topic