Skip to main content

Display Current Date in Java GUI

Screenshot
Body
This is my other java tutorial that i will teach you how to load and display the current date in Java GUI. Of course, it is a too little hassle because java has complicated codes in showing current date compared to other languages such as visual basic. Now, let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of DateText.java. 2. Import the following packages below.
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
3. Initialize your variable because we will just use JTextField here that will hold the value of the current date.
  1. JTextField dateField;
4. Create your constructor named DateText(). This will instantiate the Main of the program.
  1. public DateText() {
  2.  
  3. dateField= new JTextField(8);
  4.  
  5. JPanel content = new JPanel();
  6. content.setLayout(new FlowLayout());
  7. content.add(dateField);
  8.  
  9. this.setContentPane(content);
  10. this.setTitle("Date");
  11. this.pack();
  12. this.setLocationRelativeTo(null);
  13. this.setResizable(false);
  14.  
  15. javax.swing.Timer t = new javax.swing.Timer(1000, new DateClockListener());
  16. t.start();
  17.  
  18. }
The code above declares that the JTextField will have only 8 characters for showing date example 12/25/2014. We add panel though, and the textfield dateField is inside of that panel. But our focus here is that we use the Timer class of the swing package because this can also instantiate the date without the initialization of time in milliseconds with the instantiation of our DateListener which will be the last step of coding the program. Then the Date will show and display as we run our program. 5. Lastly, create another class that will implement an ActionListener of the date named DateListener .
  1. class DateListener implements ActionListener {
  2. public void actionPerformed(ActionEvent e) {
  3.  
  4. Calendar now = Calendar.getInstance();
  5. int month = now.get(Calendar.MONTH);
  6. int day = now.get(Calendar.DAY_OF_MONTH);
  7. int year = now.get(Calendar.YEAR);
  8. dateField.setText("" + (month + 1) + "/" + day + "/" + year);
  9.  
  10. }
  11. }
We used the calendar class from the java.util package. Calendar.MONTH for getting the current month, Calendar.DAY_OF_MONTH for getting the current day, and Calendar.YEAR syntax for getting the current year. Then it will be displayed on our textfield when we run the program. Output: output Here's the full code of this tutorial:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5.  
  6.  
  7. class DateText extends JFrame {
  8.  
  9. JTextField dateField;
  10.  
  11. public static void main(String[] args) {
  12. JFrame dte = new DateText();
  13. dte.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. dte.setVisible(true);
  15. }
  16.  
  17. public DateText() {
  18.  
  19. dateField= new JTextField(8);
  20.  
  21. JPanel content = new JPanel();
  22. content.setLayout(new FlowLayout());
  23. content.add(dateField);
  24.  
  25. this.setContentPane(content);
  26. this.setTitle("Date");
  27. this.pack();
  28. this.setLocationRelativeTo(null);
  29. this.setResizable(false);
  30.  
  31. javax.swing.Timer t = new javax.swing.Timer(1000, new DateListener());
  32. t.start();
  33.  
  34. }
  35.  
  36. class DateListener implements ActionListener {
  37. public void actionPerformed(ActionEvent e) {
  38.  
  39. Calendar now = Calendar.getInstance();
  40. int month = now.get(Calendar.MONTH);
  41. int day = now.get(Calendar.DAY_OF_MONTH);
  42. int year = now.get(Calendar.YEAR);
  43. dateField.setText("" + (month + 1) + "/" + day + "/" + year);
  44.  
  45. }
  46. }
  47.  
  48.  
  49. }
Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Visit and like my page on Facebook at: https://www.facebook.com/BermzISware Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Add new comment