how to display date and time in java which can auto-update every second

There are several methods to display date and time in java. This tutorial shows you how to display date and time in java that can auto-update every second. You can follow this video tutorial and source code included in this post.

A video tutorial for how to display date and time in java which can auto-update every second

Do you need help to remotely set up my any project on your machine or customize any project with your requirement please contact  syntech1994@gmail.com

Source code

import java.util.Calendar;
import java.util.GregorianCalendar;


public void clock()
     {
         Thread clock=new Thread()
                 {
                 public void run()
                 {
                     try {
                    while(true)
                        {
                         Calendar cal=new GregorianCalendar();
                         int day=cal.get(Calendar.DAY_OF_MONTH);
                         int month=cal.get(Calendar.MONTH)+1;
                         int year=cal.get(Calendar.YEAR);                   
                    int second=cal.get(Calendar.SECOND);
                    int minute=cal.get(Calendar.MINUTE);
                    int hour=cal.get(Calendar.HOUR);

                    lbltime.setText( hour +":"+ minute +":"+ second);
                    lblDate.setText( year +"-"+ month +"-"+ day );
                  //  getTime=lbltime.getText();
                   // getDate=lbldate.getText();
                                    sleep(1000);
                    }
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(rootPane, e);


                }
            }
        };
    clock.start();
}

Put this java method inside of java project. You can get date and time to the variable or display it on JFrame.

The technologies used in display date and time in java project

  • Java: All codes have been written using the java programming language.
  • Netbeans: This project has developed inside of the Netbeans IDE.

Check more projects>>

Be the first to comment

Leave a Reply

Your email address will not be published.


*