Dennis QT
Dennis QT

Reputation: 125

How to get updated time in Java ?

I am trying to write a simple java program which enable me to get the updated time for every seconds. All the time values are then stored in a text file. However, I am only able to get the first time and it keeps on get the same result again & again.

Code:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class testTime extends javax.swing.JFrame {

    public Writer writer=null;
    public Calendar cal = Calendar.getInstance();
    public File file;
    protected boolean isRunning=false;

    public testTime() {
        initComponents();
        initTimer();
    }

    public void initTimer()
    {
      this.isRunning=true;  
      tryToGetUpdateTime();
    }

    public void tryToGetUpdateTime()
    {
        java.awt.EventQueue.invokeLater(new Runnable() {

        SimpleDateFormat sdfHour = new SimpleDateFormat("HH:mm:ss");
        SimpleDateFormat sdfMonth = new SimpleDateFormat("dd/MM/yyyy");   

         public void run() {
            while (isRunning) {                   
                try{
                    file= new File("c:/Users/user/Desktop/updateTime.txt");

                    if(!file.exists())
                    {
                       file.createNewFile();
                    }

                    FileWriter fileWriter = new FileWriter(file,true);
                    BufferedWriter bufferWritter = new BufferedWriter(fileWriter);                                                     bufferWritter.append("**************************************************" +  '\n');
                    bufferWritter.append(sdfMonth.format(cal.getTime()) + " " + sdfHour.format(cal.getTime()) + '\n');
                    bufferWritter.append("**************************************************" +  '\n');
                    bufferWritter.close();
                   }catch(IOException ex){
                       System.err.println("Error in Writer : " + ex);
                   }              
              }
        }
      });
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new testTime().setVisible(true);
            }
        });
    }                  
}

Output:

**************************************************
15/07/2015 23:28:52
**************************************************
**************************************************
15/07/2015 23:28:52
**************************************************
**************************************************
15/07/2015 23:28:52
**************************************************

Upvotes: 1

Views: 83

Answers (2)

Max
Max

Reputation: 41

Have you tried doing the following code

Calendar cal = Calendar.getInstance();

inside tryToGetUpdateTime()

Upvotes: 0

Dragan Bozanovic
Dragan Bozanovic

Reputation: 23552

When Calendar is created, its date is not changed unless you explicitly change it, so it will always return the same value.

Secondly, it seems to me that you are instantiating the whole heavyweight Calendar only to take Date from it. There is no need for that, instantiate the Date directly:

Date date = new Date();
bufferWritter.append(sdfMonth.format(date) + " " + sdfHour.format(date) + '\n');

Upvotes: 2

Related Questions