---
title: "How to update Java timer every second?"  
description: "How to update Java timer every second?"  
author: "Allen Scott"  
published: 2014-11-04  
updated: 2014-11-04  
canonical: https://www.mindstick.com/forum/2512/how-to-update-java-timer-every-second  
category: "java"  
tags: ["swing", "date", "time", "timer"]  
reading_time: 1 minute  

---

# How to update Java timer every second?

I want to grab the [current date](https://www.mindstick.com/forum/323/find-the-day-name-and-month-name-from-current-date) and time from the [system](https://www.mindstick.com/articles/23411/the-most-effective-method-to-find-the-perfect-small-business-phone-system-for-your-business) which i can do with this code:\

```
private void GetCurrentDateTimeActionPerformed(java.awt.event.ActionEvent evt){                                                        DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");     Date date = new Date();     CurrentDateTime.setText(dateandtime.format(date));}
```

Doing this is fine as it will grab he current date nad time no [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic), however it is not [dynamic as](https://www.mindstick.com/forum/12679/change-event-not-work-as-dynamic-as-expected) the time will not [update](https://www.mindstick.com/forum/156353/what-is-hummingbird-update) unless the [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) is pressed again. So I was wondering how I could make this button more dynamic by updating the [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) every [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) to [refresh](https://www.mindstick.com/forum/12865/how-to-refresh-dropdownlist-but-keep-old-selected-value-json) the time.\

## Replies

### Reply by Anonymous User

You can use an executor to update that periodically. Something like this:

```
ScheduledExecutorService e= Executors.newSingleThreadScheduledExecutor();
e.scheduleAtFixedRate(new Runnable() {
  @Override
  public void run() {
    SwingUtilities.invokeLater(new Runnable() {
       DateFormat dateandtime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
       Date date = new Date();
       CurrentDateTime.setText(dateandtime.format(date));
    });
  }
}, 0, 1, TimeUnit.SECONDS);
```


---

Original Source: https://www.mindstick.com/forum/2512/how-to-update-java-timer-every-second

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
