Results 1 to 6 of 6
  1. #1
    murali18 is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Red face Need help with Process execution time display

    Hi All,

    I am new to Java and I am facing problem in displaying the process execution time concurrently while execution is taking place.

    I want to diplay time in format mm:ss while the execution is taking place. For the same, i have written code snippet like this,

    <----
    String s;
    long ref = System.currentTimeMillis();
    javax.swing.Timer timer2;
    SimpleDateFormat timef = new SimpleDateFormat("mm:ss");

    timer = new javax.swing.Timer(1000, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    s = timef.format(new Date(System.currentTimeMillis() - ref));
    Elapsed.setText("Elapsed Time = " + s);
    }
    });
    timer.start();

    ---->

    Although, I am able to see the Elapsed time, it is always starting from 30:00 . I want to know where I am making a mistake. Would appreciate if you can point it out to me.

    Thanks!
    Last edited by murali18; 03-18-2010 at 04:27 PM. Reason: missed one parameter (ref) declaration

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You've got a bug somewhere else in your code.

    We know this since if we user the code you've posted in a simple program, it works:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.swing.*;
    
    public class TimeDisplay {
      static long ref = System.currentTimeMillis();
    
      private static void createAndShowGUI() {
        final SimpleDateFormat timef = new SimpleDateFormat("mm:ss");
        final JLabel elapsedLabel = new JLabel("Elapsed Time = " + timef.format(new Date(0)),
            SwingUtilities.CENTER);
    
        Timer timer = new javax.swing.Timer(1000, new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                Date currentDate = new Date(System.currentTimeMillis() - ref);
                String formattedDate = timef.format(currentDate);
                elapsedLabel.setText("Elapsed Time = " + formattedDate);
              }
            });
        timer.start();
    
        JPanel panel = new JPanel(new BorderLayout());
        panel.setPreferredSize(new Dimension(400, 200));
        panel.add(elapsedLabel);
    
        JFrame frame = new JFrame("TimeDisplay");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
    
      }
    }

    So logic tells us that your error is somewhere else in code that you haven't posted. So you'll need to do a little debugging here to try to isolate the offending code.

    Much luck.
    Last edited by Fubarable; 03-18-2010 at 04:48 PM.

  3. #3
    murali18 is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default hi

    I executed your program... Still getting the same problem!

    Its always starting from 30:00 (like 30:00, 30:01,30:02 and so on...). Has it got something to do with system time?

    By the way, attaching screen shot of output of the above program..... I know, I might be stretching people here but I can't seem to get it right...Nevertheless, appreciate your help!
    Attached Thumbnails Attached Thumbnails Need help with Process execution time display-testrun.bmp  

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    On further reflection, I think that I was wrong above. You and I probably shouldn't use Date to represent elapsed time as Date represents a point in time, not elapsed time (see this for more detail).

    Perhaps just getting the system time and doing the math yourself would work better:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class TimeDisplay {
      static long ref = System.currentTimeMillis();
      static String formatString = "%02d:%02d";
    
      private static void createAndShowGUI() {
        final JLabel elapsedLabel = new JLabel("Elapsed Time = " + String.format(formatString, 0, 0),
            SwingUtilities.CENTER);
    
        Timer timer = new javax.swing.Timer(1000, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            long currentDate = System.currentTimeMillis();
            long elapsedSeconds = (currentDate - ref)/1000;
            int minutes = (int) (elapsedSeconds / 60);
            int seconds = (int)(elapsedSeconds % 60);
            String formattedTime = String.format(formatString, minutes, seconds);
            elapsedLabel.setText("Elapsed Time = " + formattedTime);
          }
        });
        timer.start();
    
        JPanel panel = new JPanel(new BorderLayout());
        panel.setPreferredSize(new Dimension(400, 200));
        panel.add(elapsedLabel);
    
        JFrame frame = new JFrame("TimeDisplay");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
    
      }
    }

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Yeah, SimpleDateFormat is locale-sensitive, and murali18 must be in a time zone that has a half-hour offset. You can still use SimpleDateFormat, but you need to use UTC time zone.

    Java Code:
            SimpleDateFormat timef = new SimpleDateFormat("mm:ss");
            timef.setTimeZone(TimeZone.getTimeZone("UTC"));
    (I haven't tried it, but it should work.)

    -Gary-

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Display process bar in cell directly with Java reporting tool
    By freezea in forum Reviews / Advertising
    Replies: 0
    Last Post: 05-10-2009, 05:11 AM
  2. Date n Time Display problem
    By kapilverma32 in forum Advanced Java
    Replies: 6
    Last Post: 02-10-2009, 02:53 PM
  3. Replies: 25
    Last Post: 06-28-2008, 06:08 PM
  4. Method execution time
    By javaplus in forum Advanced Java
    Replies: 3
    Last Post: 11-26-2007, 10:51 AM
  5. How can I set a time out on the loading process
    By paul in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 05:54 AM

Posting Permissions

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