364,083 Members | 5932 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

System Time on Jframe

samsmile62000
P: 5
how can i get system date and time on my Jframe.
Jan 25 '12 #1
Share this Question
Share on Google+
3 Replies


rotaryfreak
P: 74
you can use the Gregorian Calendar class to create an instance of that class and then extract the data that you need from that object

http://docs.oracle.com/javase/6/docs...nCalendar.html

once that is done, you should probably create a JLabel and set the text as the date and time that you want. You will then add it to a JPanel inside your JFrame.

Something like this:



Expand|Select|Wrap|Line Numbers
  1.  
  2. class JFrameDate{
  3. public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
  4.  
  5. //this method gets the date and time now. you can modify the
  6. //date format to not show the time
  7.  
  8. public static String timeNow() {
  9.      Calendar cal = Calendar.getInstance();
  10.      SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
  11.      return sdf.format(cal.getTime());
  12. }
  13.  
  14. public static void main(String [] args){
  15.      JFrame myFrame = new JFrame("Show Date");
  16.      myFrame.setVisible(true);
  17.  
  18.      JPanel myPanel = new JPanel();
  19.  
  20.      JLabel dateTime = new JLabel();
  21.      dateTime.setText(JFrameDate.timeNow());
  22.  
  23.      myPanel.add(dateTime);
  24.  
  25.      myFrame.add(myPanel);
  26.  }
  27. }
  28.  
i have not tested this code, but the idea is straight forward. You will have to adjust your JFrame and JPanels to the layout, size and display location you want, but i think this is a good starting point
Jan 27 '12 #2

samsmile62000
P: 5
hey i got an error .... cannot find symbol SimpleDateFormat class
Jan 27 '12 #3

samsmile62000
P: 5
het thanks got a solution.... it worked..thanks
Jan 27 '12 #4

Post your reply

Help answer this question



Didn't find the answer to your Java question?

You can also browse similar questions: Java