Display Current Date in Java GUI
Submitted by donbermoy on Tuesday, May 6, 2014 - 03:40.
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.
3. Initialize your variable because we will just use JTextField here that will hold the value of the current date.
4. Create your constructor named DateText(). This will instantiate the Main of the program.
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 .
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:
Here's the full code of this tutorial:
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:donbermoy@engineer.com
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.*;
- JTextField dateField;
- public DateText() {
- content.add(dateField);
- this.setContentPane(content);
- this.setTitle("Date");
- this.pack();
- this.setLocationRelativeTo(null);
- this.setResizable(false);
- t.start();
- }
- dateField.setText("" + (month + 1) + "/" + day + "/" + year);
- }
- }

- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.*;
- JTextField dateField;
- dte.setVisible(true);
- }
- public DateText() {
- content.add(dateField);
- this.setContentPane(content);
- this.setTitle("Date");
- this.pack();
- this.setLocationRelativeTo(null);
- this.setResizable(false);
- t.start();
- }
- dateField.setText("" + (month + 1) + "/" + day + "/" + year);
- }
- }
- }
Add new comment
- 1353 views