Load the current time that tick every seconds in Java

This tutorial entitled “Load the current time that tick every seconds in Java” will teach you on how to load the current time and change automatically in every seconds. I already publish about loading the current time, but this tutorial is different.  Every tick of seconds, the time also changes automatically.

The program use java jLabel element to display the changeable time. Please follow all the steps to complete this tutorial.

Load the current time that tick every seconds in Java Steps

  1. Add or create a new form inside your Java project. In my case, I named my form using “DisplayTimeInJava”.

2. Design your form just look like what I do in the image below. The elements are located in your Java pallets in the right side your Netbeans window.

3. Copy all the codes below and paste it above your main class. The codes are used to access the required libraries in this program.

[java]import java.util.Calendar;
import java.util.GregorianCalendar;[/java]

4. Copy the line of codes below and insert it into your main class but outside from other methods. The codes are used for integer variable declaration.

[java]int timerun = 0;[/java]

5. Inside your main class, create a new method. In my case, I used “load_clock” as my method name.

[java]public final void load_clock(){

}[/java]

6. Copy the following codes below and paste it inside your new created methods. The codes are composed of thread, calendar, and other variables use to load the time.

[java]new Thread(){
public void run(){
while (timerun ==0){
Calendar cal = new GregorianCalendar();

int hour = cal.get(Calendar.HOUR);
int min = cal.get(Calendar.MINUTE);
int sec = cal.get(Calendar.SECOND);
int am_pm = cal.get(Calendar.AM_PM);

String day_night = “”;
if(am_pm == 1)
{
day_night = “PM”;
}else
{
day_night = “AM”;
}

String time = hour + “:” + min + “:” + sec + ” ” + day_night;
jLabel1.setText(time);
}
}
}.start();[/java]

7. Copy the method name of your new created method and paste it inside your public method after the “initComponent” variable. Public method is use to initialized and load all the functions in form load event of the program.

[java]load_clock();[/java]

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

About The Load the current time that tick every seconds In Java

Project Name: Load the current time that tick every seconds
Language/s Used: JAVA
Database: None
Type: Desktop Application
Developer: IT SOURCECODE
Updates: 0
Load the current time that tick every seconds In Java– Project Information

After completing this tutorial, you are now understand on how to create a program that load the current time and change it automatically based on the timer tick. If you have questions and suggestions, feel free to contact me through [email protected] or you can use the contact information of this website.

Related Articles You May Like:

Leave a Comment