System Time on Jframe

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samsmile62000
    New Member
    • Jan 2012
    • 5

    System Time on Jframe

    how can i get system date and time on my Jframe.
  • rotaryfreak
    New Member
    • Oct 2008
    • 74

    #2
    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



    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:



    Code:
    class JFrameDate{
    public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
    
    //this method gets the date and time now. you can modify the
    //date format to not show the time
    
    public static String timeNow() {
         Calendar cal = Calendar.getInstance();
         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
         return sdf.format(cal.getTime());
    }
    
    public static void main(String [] args){
         JFrame myFrame = new JFrame("Show Date");
         myFrame.setVisible(true);
    
         JPanel myPanel = new JPanel();
    
         JLabel dateTime = new JLabel();
         dateTime.setText(JFrameDate.timeNow());
    
         myPanel.add(dateTime);
         
         myFrame.add(myPanel);
     }
    }
    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

    Comment

    • samsmile62000
      New Member
      • Jan 2012
      • 5

      #3
      hey i got an error .... cannot find symbol SimpleDateForma t class

      Comment

      • samsmile62000
        New Member
        • Jan 2012
        • 5

        #4
        het thanks got a solution.... it worked..thanks

        Comment

        Working...