Load the Current Date and Time in Java

0
52

This tutorial is about how to Load Current Date/Time Using Java. This tutorial will help you on how to load the system current date and time through label element in Java. The program will display the current date and time base from the computer system.

Some of the computer systems are using the date and time to track and identify the program transactions. The program includes Point of Sale System, Inventory System, Billing System and much more.

Load Current Date/Time Using Java

1.Create/Add a new JFrame Form inside your source package.

2. Design your Form just like what I do in the image below.

3. Add the following import codes above your class to access the required libraries.

  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. import java.util.GregorianCalendar;

4. Create a new method inside your class.

  1. public final void loadDateTime(){
  2.  
  3. }

5. Insert the following codes inside your new created method.

  1. //load date/time
  2. Date date = GregorianCalendar.getInstance().getTime();
  3. Date time = GregorianCalendar.getInstance().getTime();
  4. SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy");
  5. SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
  6. jLabel2.setText(sdf2.format(time));
  7. jLabel1.setText(sdf.format(date));

6. Copy the method name and insert it into your public method inside your class.

  1. public LoadSystemDateTime() {
  2. initComponents();
  3. loadDateTime();
  4. }

7. Run your program and the output should look like the image below.

Remember that the computer system date and time will be displayed in the program you created whether it is updated or outdated. If you have comments and question about Load Current Date/Time Using Java. Feel free to use our contact information.

Related Articles You May Like:

If you find this article useful, you can share this to your friends.

Facebook Comments
(Visited 49 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here