current time in java – Kensoft PH https://kensoftph.com Power up your knowledge in programming Thu, 29 Sep 2022 10:11:59 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 https://kensoftph.com/wp-content/uploads/2021/07/cropped-Kensoftph-New-logo-32x32.png current time in java – Kensoft PH https://kensoftph.com 32 32 How to get current time in Java | 100% Best for Beginners https://kensoftph.com/how-to-get-current-time-in-java/ https://kensoftph.com/how-to-get-current-time-in-java/#respond Wed, 21 Jul 2021 05:09:11 +0000 https://kensoftph.com/?p=865 In Java, there are several methods to get the current time. You will learn how to get current time in Java in this article. This is the ideal tutorial for you if you’re having trouble getting the current time in Java. Continue reading to find out more, and then let’s get started.

So, I’ll show you two examples of how to retrieve the current time in Java Swing and JavaFX. I’ll also show you how to keep track of time in real time.get current time in java

How to get current time in Java

It is very easy to get current time in Java, and we will use the simplest method for you as a beginner. In Java, we’ll use the SimpleDateFormat class to get the current time. It has the ability to format the date and time. So, in this tutorial. We will focus on getting the current time in Java. Time format are specified by time pattern strings. The following pattern letters are for time only.

LetterTime pattern or componentPresentationExamples
aAm/pm markerTextPM
HHour in day (0-23)Number0
kHour in day (1-24)Number24
KHour in am/pm (0-11)Number0
hHour in am/pm (1-12)Number12
mMinute in hourNumber30
sSecond in minuteNumber50
SMillisecondNumber978
zTime zoneGeneral time zonePacific Standard Time; PST; GMT-08:00
ZTime zoneRFC 822 time zone-0800
XTime zoneISO 8601 time zone-08; -0800; -08:00
SimpleDateFormat Class Time patterns

The time pattern strings in the SimpleDateFormat() constructor should be interpreted as follows. Please see the code example below.

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");

For interpretation, you should encapsulate the time patterns in double quotes. Put a single quote inside the double quote to avoid misinterpretation. The characters within a single quote are not interpreted as a time pattern. The result would be an uninterpreted string. Take a look at the code below for an example.

SimpleDateFormat sdf = new SimpleDateFormat("'Time now: 'hh:mm");

Output

Time now: 1:23

It’s simple to get the current time in Java with an AM/PM marker. Simply add “a” in the SimpleDateFormat() parameter.

SimpleDateFormat sdf = new SimpleDateFormat("'Time now: 'hh:mm a");

Output

Time now: 1:23 PM

I’ll show you two examples, the first of which is getting the current time in Java Swing and running it in real-time. Another example is a JavaFX application that displays the current time in real time. We’ll use a thread to keep our clock operating in real time.

How to use Java Swing to get the current time in real-time

In this example we will use a Java Swing code to get current time in Java Swing running in real-time. Now, take a look at the example code below to get current time in Java.

Example 1

private void time(){
   Thread thread = new Thread(){
       public void run(){
           try{
                SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
                System.out.print(sdf.format(new Date)); //This will display the current time
                Thread.sleep(1000); //The thread will sleep every second
           }catch(Exception e){
                System.out.print(e);
           }
       }
   };
   thread.start(); //You should start the thread to make our time running.
}

How to get current time in JavaFX running in real-time

In this example we will use a JavaFX code to get current time and running it in real-time in JavaFX. Now, take a look at the example code below to get current time in Java. 

To stop a running thread in JavaFX, we must declare a global variable of the data type boolean. This private volatile boolean stop = false; should be the global variable. This example is based on a JavaFX undecorated stage. So, to stop the running thread in JavaFX. You need to call the the stop variable at the close button in your JavaFX application. 

@FXML
private void Close_clicked(MouseEvent event){
    stop = true;
    javafx.application.Platform.exit();
}

If you call the stop = true; in your close button, it will stop the running thread in your JavaFX application.

Example 2

This is the code to get current time in JavaFX application.

private void Timenow(){
    Thread thread = new Thread(() -> {
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
        while(){
            try{
                Thread.sleep(1000);
            }catch(Exception e){
                System.out.println(e);
            }
            final String timenow = sdf.format(new Date());
            Platform.runLater(() -> {
                 time.setText(timenow); // This is the label
            });
        }
    });
    thread.start();
}

get current time in java

YouTube Video

]]>
https://kensoftph.com/how-to-get-current-time-in-java/feed/ 0 JavaFX Tutorial: Show current time in Java application nonadult