Results 1 to 9 of 9
- 11-20-2008, 10:03 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
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:
I don't think I've used 'System.currentTimeMillis();' correctly.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; }
Can anyone please help me with this.
If there is another way please tell me.
Thanks
- 11-20-2008, 10:07 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
What is wrong with it? What are you getting, and what are you expecting?
- 11-20-2008, 10:14 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
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
- 11-20-2008, 10:20 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
By using SimpleDateFormat and newDate(long).
- 11-20-2008, 10:36 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
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
- 11-20-2008, 10:44 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Here a small example
And there is a reason for the "HH" instead of "hh", and for setting the TimeZone.Java Code:SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); System.out.println(sdf.format(new Date(7635L)));
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.
- 11-20-2008, 12:32 PM #7
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
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. "
What is wrong with the code in my method? ANDJava 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; }
Why is the output given as 'java.text.SimpleDateFormat@85c13e85'??
please help...
- 11-20-2008, 12:42 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 11-20-2008, 01:15 PM #9
Member
- Join Date
- Nov 2008
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Calculating per hour statistics
By vipergt89 in forum New To JavaReplies: 2Last Post: 05-01-2008, 06:25 AM -
Calculating trigonometric functions
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:56 PM -
Calculating hyperbolic functions
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:55 PM -
Calculating the DB connection time
By Java Tip in forum Java TipReplies: 0Last Post: 01-20-2008, 08:55 AM -
Calculating sin of a double value
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 08:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks