Results 1 to 5 of 5
  1. #1
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Using Date() to get Start Time and Finish Time

    I have a single file copy that I want to record the start time and the finish time for. The code below indeed provide me with time, but the start and finish are the same. I know for a fact that it takes about 34 seconds for this particular file to copy from the source path to the destination path. I timed it with a stopwatch. If anything, my seconds under the start time should be different that the seconds for the finish time. Could another set of eyes tell what I am doing wrong?

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // Copy File and capture the start time and finish time then display
            String url1 = jTextField3.getText();
            String url2 = jTextField1.getText();
            Date dNow = new Date();
            SimpleDateFormat ft = new SimpleDateFormat("M/dd/yy   hh:mm:ss a");
            
            Path path1 = Paths.get(url1);
            Path path2 = Paths.get(url2);
            
            jLabel2.setText(ft.format(dNow)); //setting start time as label text
            try
            {        
                Files.copy(path1, path2);        
            }
            catch(IOException e)
            {
                JOptionPane.showMessageDialog(null, "There was a problem");
                e.printStackTrace();
            }
            jLabel3.setText(ft.format(dNow)); //setting finish time as label text        
        }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Using Date() to get Start Time and Finish Time

    A Date object is not dynamic. By that I mean if you create a Date object, then 5 minutes later it will still hold the same date and time. it does not "tick" over. Perhaps you should create a second Date object at the end and then compare the two to see if there is some time difference.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,357
    Rep Power
    6

    Default Re: Using Date() to get Start Time and Finish Time

    Or just use System.currentTimeMillis() in stead of Date; when you take two snapshots you can easily get the difference in milliseconds, which you can easily translate to seconds.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Using Date() to get Start Time and Finish Time

    Also at jpf.com

  5. #5
    dalythe is offline Member
    Join Date
    Mar 2013
    Posts
    27
    Rep Power
    0

    Default Re: Using Date() to get Start Time and Finish Time

    I found out what my problem was. I didn't need the variable dNow at all. All I had to do is call the Date() method whenever I needed the current time and date, then assign those times to variables. Then I could plug in that specific date and time where I wanted it. Thanks for the help.

Similar Threads

  1. Displaying Date and Time in GUI
    By no_slip_condition in forum New To Java
    Replies: 10
    Last Post: 06-09-2013, 05:00 PM
  2. Date vs Time
    By mikerada6 in forum New To Java
    Replies: 1
    Last Post: 10-29-2010, 01:34 AM
  3. converting string (GMT date) to date in US time.
    By JRuyechan in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 08:07 AM
  4. Time and Date
    By Manfizy in forum New To Java
    Replies: 0
    Last Post: 05-27-2009, 01:58 PM
  5. how to get the current date and time
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 07:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •