title: Comparing current date time(live) with file last modified
body: <p>I want my program output such that, whenever it receives a text file from a client, It will print "A new file has been received!". The text files will be store in C:/Users/%UserProfile%/Desktop/ad.</p>
  
  <p>The code below is to check whether a new text file has been received into the
  directory.</p>
  
  <pre><code>import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.File;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  import java.util.Date;
  
  import javax.swing.Timer;
  
  
  public class Test {
  
      final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      static int count = 0;
      static File[] f = null;
      static Date date = new Date();  
      static Calendar now = null;
  
      public static void main(String[] args) {
          DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
          while(true) {
              try {
                  Thread.sleep(2000);
              } catch (InterruptedException e1) {
                  e1.printStackTrace();
              }
              f = new File("C:/Users/roberts/Desktop/ad").listFiles();
              int l = f.length;
              System.out.println("There are " +  l +  " files in the directory");
  
              for (int i = 0; i &lt; l; i++) {
                  System.out.println("file " + i +  " date modified " + dateFormat.format(f[i].lastModified()));
              }
          }
      }
  }
  </code></pre>
  
  <p>Output of total number of files in the directory initially = 1 and last modified</p>
  
  <p><img src="https://i.stack.imgur.com/lzv72.png" alt="enter image description here"></p>
  
  <p>Output of total number of files in the directory after file is received = 2 and last modified</p>
  
  <p><img src="https://i.stack.imgur.com/VoPaU.png" alt="enter image description here"></p>
  
  <p>Now in order to print "A new file has been received!" I need to compare the current date time with the last modified date time of the file.</p>
  
  <p>This is what I did</p>
  
  <pre><code>import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.File;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  import java.util.Date;
  
  import javax.swing.Timer;
  
  
  public class Test {
  
      final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      int interval = 1000;
      static int count = 0;
      static File[] f = null;
      static Date date = new Date();  
      static Calendar now = null;
  
      public static void main(String[] args) {
          DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
          while(true) {
  
                  f = new File("C:/Users/roberts/Desktop/ad").listFiles();
                  int l = f.length;
                  System.out.println(l);
                  new Timer(1000, new ActionListener() {
                      @Override
                      public void actionPerformed(ActionEvent e) {
                          now = Calendar.getInstance();
                          for (int i = 0; i &lt; l; i++) {
                              if(dateFormat.format(now.getTime()).equals(dateFormat.format(f[i].lastModified()))) {
                                  System.out.println("A new file has been received!");
                              }
                          }
  
                      }
                  }).start();
  
              try {
                  Thread.currentThread().join();
              } catch (InterruptedException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>I tried but it doesn't print "A new file has been received!". Please help</p>