Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Using Date() to get Start Time and Finish Time of a copyFiles method

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Using Date() to get Start Time and Finish Time of a copyFiles method

    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?

    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
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Using Date() to get Start Time and Finish Time of a copyFiles method

    dNow is never given a different value after it is initialised.

    You could do away with the variable altogether and say

    jLabel2.setText(ft.format(new Date()));
    // some code
    // and later...
    jLabel3.setText(ft.format(new Date()));

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    dalythe (June 17th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using Date() to get Start Time and Finish Time of a copyFiles method

    Quote Originally Posted by pbrockway2 View Post
    dNow is never given a different value after it is initialised.

    You could do away with the variable altogether and say

    jLabel2.setText(ft.format(new Date()));
    // some code
    // and later...
    jLabel3.setText(ft.format(new Date()));
    I tried this and it was acting as if I didn't import Date class.

    --- Update ---

    I imported java.text.* and java.util.* and I get a red exclamation mark stating that it cannot find Date() method.

    --- Update ---

    I saw what I was doing wrong. I needed to type new Date() versus just Date(). Thanks for your help.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Using Date() to get Start Time and Finish Time of a copyFiles method

    You're welcome.

Similar Threads

  1. Parsing a full date/time/timezone date to "yyyy-MM-dd"
    By Occidentally in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 4th, 2012, 08:57 AM
  2. Date and Time Picker
    By Hrithik in forum Java Theory & Questions
    Replies: 3
    Last Post: September 25th, 2012, 09:55 AM
  3. [SOLVED] how to get phone time and date
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: August 26th, 2009, 12:15 PM
  4. How to Get the current date and time
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 2nd, 2008, 01:55 PM
  5. How to Get the current date and time
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: December 2nd, 2008, 01:55 PM