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.
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.
Letter | Time pattern or component | Presentation | Examples |
a | Am/pm marker | Text | PM |
H | Hour in day (0-23) | Number | 0 |
k | Hour in day (1-24) | Number | 24 |
K | Hour in am/pm (0-11) | Number | 0 |
h | Hour in am/pm (1-12) | Number | 12 |
m | Minute in hour | Number | 30 |
s | Second in minute | Number | 50 |
S | Millisecond | Number | 978 |
z | Time zone | General time zone | Pacific Standard Time; PST; GMT-08:00 |
Z | Time zone | RFC 822 time zone | -0800 |
X | Time zone | ISO 8601 time zone | -08; -0800; -08:00 |
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(); }