how can i get system date and time on my Jframe.
System Time on Jframe
Collapse
X
-
Tags: None
-
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:
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 pointCode: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); } } -
-
Comment