Results 1 to 9 of 9
  1. #1
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Question Need help with calculating the time the user...

    Hi, I please need help to calculate the time the user used my program for.

    I'm not sure if my code is correct, I used 'System.currentTimeMillis();'

    Here is my code for calculating the programRuntime:

    Java Code:
    SimpleDateFormat format2 = new SimpleDateFormat("hh:mm:ss a");
    	    static long timeIn = new Date().getTime();
    	    
    	    
    public static long programRuntime() {
    long timeInD = timeIn;
    	  
    long timeOutD = System.currentTimeMillis();
    
    long msec = timeOutD - timeInD;
    	  
    return msec;
    }
    I don't think I've used 'System.currentTimeMillis();' correctly.
    Can anyone please help me with this.
    If there is another way please tell me.
    Thanks

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    What is wrong with it? What are you getting, and what are you expecting?

  3. #3
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Question Need help with calculating the time the user...

    I just want to know if I've used the 'System.currentTimeMillis();' method correctly? Because I get a funny answer. e.g. "you have used the program for 7375 "

    Is this answer 7.3 seconds ??

    If so how can I get it in 'hh:mm:ss'?

    Thank you

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    By using SimpleDateFormat and newDate(long).

  5. #5
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Question

    I'm not sure how to do this.

    I tried it but now the output is "you have used this program for: Thu Jan 01 02:00:03 GMT+02:00 1970"...

    Why is this??

    [/CODE]
    public static Date programRuntime() {
    long timeInD = timeIn;

    long timeOutD = System.currentTimeMillis();

    long msec = timeOutD - timeInD;

    SimpleDateFormat format3 = new SimpleDateFormat("hh:mm:ss a");
    Date time = new Date(msec);

    return time;

    }
    [CODE]

    Can you please help

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Here a small example

    Java Code:
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println(sdf.format(new Date(7635L)));
    And there is a reason for the "HH" instead of "hh", and for setting the TimeZone.

    Edit: And, obviously, it does not work for things that take longer than a day. If that may be the case, you're going to have to do the math yourself.
    Last edited by masijade; 11-20-2008 at 10:48 AM.

  7. #7
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

    Question

    I tried to code the 'program runtime' using SimpleDateFormat and newDate(long), and looking at the examlpe you gave me.

    But now when I run my program the output is:

    "00:00:07.365 (the correct time is thrown out here but then it gives me this...)

    Jo, thank you for using this program, you have used the program for: java.text.SimpleDateFormat@85c13e85
    the program will now exit, Goodbye. "

    Java Code:
    public static SimpleDateFormat programRuntime() {
    	    	long timeInD = timeIn;
    	  
    	    	long timeOutD = System.currentTimeMillis();
    
    	    	long msec = timeOutD - timeInD;
    	  
    	    	SimpleDateFormat format3 = new SimpleDateFormat("HH:mm:ss.SSS");
    	    	sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    	    	System.out.println(format3.format(new Date(msec)));
    		    
    			return sdf;
    			
    	    }
    What is wrong with the code in my method? AND
    Why is the output given as 'java.text.SimpleDateFormat@85c13e85'??
    please help...

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because you have not a clue what you're doing.

    Why are you returning the SinmpleDateFormat object? That is only for formatting the String representations of Dates. You do know that a Date is just a number and it has, itself, no format. You only format the Human Readable String representation of a Date. Why don't you return a String? Namely the one that you are currently printing.

    Also, your edits of my code are not complete, as you forgot to replace sdf with format3 in two different places, so the code you posted wouldn't compile anyway.

    Pay attention to what you're doing, as attention to detail is the programmers biggest ally or foulest foe.

  9. #9
    Smirre is offline Member
    Join Date
    Nov 2008
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Calculating per hour statistics
    By vipergt89 in forum New To Java
    Replies: 2
    Last Post: 05-01-2008, 06:25 AM
  2. Calculating trigonometric functions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:56 PM
  3. Calculating hyperbolic functions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:55 PM
  4. Calculating the DB connection time
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:55 AM
  5. Calculating sin of a double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:13 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
  •