Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-12-2006, 06:22 AM   PM User | #1
Yakisoba
Regular Coder

 
Join Date: Feb 2005
Location: Tokyo, Japan
Posts: 151
Thanks: 0
Thanked 0 Times in 0 Posts
Yakisoba is an unknown quantity at this point
Java - Timer - with stop watch like display

I would like to time (and display) how long it takes my program to preform a certain task...

Actually, I have already accomplished this but with one issue:

my timer is displayed in a label 00:00:00

when I start the timer the first second appears like this: 09:00:00
then: 09:00:01
then: 09:00:02
then: 09:00:03
...
...
...

and seems to work fine.

Why is the first second displayed as being 9 hours long? How do I correct this issue?

here is the code for my timer:
Code:
    long count = 0;
    private final SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
    Timer stopWatch = new Timer(1000, new ActionListener(){
        public void actionPerformed(ActionEvent event){
            timerDisplay.setText(TIME.format(new Date(count++ * 1000)));
        }
    });
Thanks,

Yak
Yakisoba is offline   Reply With Quote
Old 10-12-2006, 07:20 AM   PM User | #2
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
I believe it's your date

Code:
new Date(count++ * 1000)
according to the api:

Quote:
public Date(long date)

Constructs a Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.

Parameters:
date - milliseconds since January 1, 1970, 00:00:00 GMT not to exceed the milliseconds representation for the year 8099. A negative number indicates the number of milliseconds before January 1, 1970, 00:00:00 GMT.
Specifically that Parameters part. I think that may be causing an issue.

To be honest I didn't believe it either but I created a short program to print out Date to see.

Code:
import java.util.*;
import java.text.*;

public class DateTest {

	public static void main(String args[])
	{
		SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
		Date d = new Date(1000);
		System.out.println(TIME.format(d));
		System.out.println(d);
	}
	
}
Output:

Code:
07:00:01
Wed Dec 31 19:00:01 EST 1969
So it looks like the method you are trying to do it wont work specifically.
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Old 10-12-2006, 08:43 AM   PM User | #3
Yakisoba
Regular Coder

 
Join Date: Feb 2005
Location: Tokyo, Japan
Posts: 151
Thanks: 0
Thanked 0 Times in 0 Posts
Yakisoba is an unknown quantity at this point
Aradon, thanks for the reply.

I originally thought it was:
Code:
new Date(count++ * 1000)
as well...

but I get the same results if I remove it.
i.e.
Code:
timerDisplay.setText(TIME.format(count++ * 1000))
the first second still registers as 9 hours...then works fine.

not much more code to look through to find the issue. Currently looking into "SimpleDateFormat" as the possible root of the problem.

so here is the code now: (not much different, no Date)
Code:
    long count = 0;
    private final SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
    Timer stopWatch = new Timer(1000, new ActionListener(){
        public void actionPerformed(ActionEvent event){
            timerDisplay.setText(TIME.format(count++ * 1000));
        }
    });
So the question still stands; why does the first second come up as 9 hours?

Any help would be greatly appreciated.

Thanks,

Yak
Yakisoba is offline   Reply With Quote
Old 10-12-2006, 11:11 AM   PM User | #4
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
My Intuition states that putting in an int / long like that the format probably converts it into a date.

Here is what I suggest. Date has basically become a deprecated class with little functionality left. So what should you use? GregorianCalendar.

Since you aren't using milliseconds, you can construct a GregorianCalendar as such:

Quote:
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second)
Constructs a GregorianCalendar with the given date and time set for the default time zone with the default locale.
So to give you an example:

Code:
import java.util.*;

public class DateTest {

	public static void main(String args[])
	{
		GregorianCalendar gc = new GregorianCalendar(0,0,0,0,0,1);
		System.out.println(gc.get(Calendar.HOUR) +" : " + gc.get(Calendar.MINUTE) + " : " + gc.get(Calendar.SECOND));
		gc = new GregorianCalendar(0,0,0,0,0,70);
		System.out.println(gc.get(Calendar.HOUR) +" : " + gc.get(Calendar.MINUTE) + " : " + gc.get(Calendar.SECOND));
	}
	
}
Which gives you output of:

Code:
0 : 0 : 1
0 : 1 : 10
All of the reasoning behind using the get and the constructor as well as many other examples are found in the api:

GregorianCalendar

Hope this helps!
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch

Last edited by Aradon; 10-12-2006 at 11:14 AM..
Aradon is offline   Reply With Quote
Old 10-13-2006, 02:22 AM   PM User | #5
daniel_g
Regular Coder

 
Join Date: Mar 2006
Posts: 258
Thanks: 1
Thanked 2 Times in 2 Posts
daniel_g is an unknown quantity at this point
Quote:
Specifically that Parameters part. I think that may be causing an issue.

To be honest I didn't believe it either but I created a short program to print out Date to see.


Code:

import java.util.*;
import java.text.*;

public class DateTest {

public static void main(String args[])
{
SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
Date d = new Date(1000);
System.out.println(TIME.format(d));
System.out.println(d);
}

}Output:


Code:
07:00:01
Wed Dec 31 19:00:01 EST 1969So it looks like the method you are trying to do it wont work specifically.
Yes sir, if you change your code to:
Code:
import java.util.*;
import java.text.*;

public class DateTest {

	public static void main(String args[])
	{
		SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
		Date d = new Date(0,0,0);
		System.out.println(TIME.format(d));
		System.out.println(d);
	}
	
}
You get the following output:
Code:
 ----jGRASP exec: java DateTest

12:00:00
Sun Dec 31 00:00:00 EST 1899

 ----jGRASP: operation complete.
Not sure how it would help Yaki, but I post it just in case.
daniel_g is offline   Reply With Quote
Old 10-13-2006, 04:50 AM   PM User | #6
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
Quote:
Originally Posted by daniel_g View Post
Yes sir, if you change your code to:
Code:
import java.util.*;
import java.text.*;

public class DateTest {

	public static void main(String args[])
	{
		SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
		Date d = new Date(0,0,0);
		System.out.println(TIME.format(d));
		System.out.println(d);
	}
	
}
You get the following output:
Code:
 ----jGRASP exec: java DateTest

12:00:00
Sun Dec 31 00:00:00 EST 1899

 ----jGRASP: operation complete.
Not sure how it would help Yaki, but I post it just in case.
Well yes, but that is a deprecated method in the Date Class :P
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Old 10-13-2006, 06:59 AM   PM User | #7
Yakisoba
Regular Coder

 
Join Date: Feb 2005
Location: Tokyo, Japan
Posts: 151
Thanks: 0
Thanked 0 Times in 0 Posts
Yakisoba is an unknown quantity at this point
Thanks for the suggestions Aradon and daniel_g,

The whole GregorianCalender solution appears to work, but seems a little cumbersome to add as my little stop watch.

I have found a solution, I had to cheat a little, but it works...

Basically I have chosen a random date and time in Milliseconds. Since I don't care about the year, month, day, etc. it doesn't matter what thier values are as long as the time initially appears 00:00:00. (the 24th hour of some random day)

I will make this a final variable that will essentially be my start time.
Code:
    long count = 80492400;
then I just increment it using the code I provided earlier.

Let me know what you think?

Yak
Yakisoba is offline   Reply With Quote
Old 10-13-2006, 09:27 AM   PM User | #8
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
Quote:
Originally Posted by Yakisoba View Post
Thanks for the suggestions Aradon and daniel_g,

The whole GregorianCalender solution appears to work, but seems a little cumbersome to add as my little stop watch.

I have found a solution, I had to cheat a little, but it works...

Basically I have chosen a random date and time in Milliseconds. Since I don't care about the year, month, day, etc. it doesn't matter what thier values are as long as the time initially appears 00:00:00. (the 24th hour of some random day)

I will make this a final variable that will essentially be my start time.
Code:
    long count = 80492400;
then I just increment it using the code I provided earlier.

Let me know what you think?

Yak
If it works, it works. In reality this is probably a faster solution then creating the Calendar. However, I'm unsure how of that number will react on different machines.

For example, when both you and I ran our seperate tests on Date you got a time that was 09 in hours while mine was at 07.

Since the time is based off of epoch January 1, 1970, 00:00:00 GMT

Now the reason for this is probably a time zone issue of some type, me being in the Eastern US and you being somewhere else.

In any case I did a test:

Code:
import java.util.*;
import java.text.*;

public class DateTest {

	public static void main(String args[])
	{
		SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
		Date d = new Date(80492400);  // Long time given
		System.out.println(d);
		d = new Date(80492400 + (1000)); // add one second
		System.out.println(d);
	}
	
}
Now I _could_ be misreading the way you are using long (which at 4:30 am is a good possibility) However I get the following results

Code:
Thu Jan 01 17:21:32 EST 1970
Thu Jan 01 17:21:33 EST 1970
As you can tell, I don't get 0:0:0 for that time in miliseconds.

Is this how you are using the code or am I missing something?

Now granted if this piece of code isn't going to any important application then it may be best to continue with the way you've done it. However if you ever plan to expand upon it or pass it to someone, it might be best to go with a more robust method.
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Old 10-16-2006, 02:41 AM   PM User | #9
Yakisoba
Regular Coder

 
Join Date: Feb 2005
Location: Tokyo, Japan
Posts: 151
Thanks: 0
Thanked 0 Times in 0 Posts
Yakisoba is an unknown quantity at this point
Aradon,

Here is the code I came up with...(I don't use "Date", that could be the difference you witnessed. To make sure, if you have a chance, can you give this code a test?).
Code:
    private static final long START_TIME = 80492401;

    long count = START_TIME;
    private final SimpleDateFormat TIME = new SimpleDateFormat("HH:mm:ss");
    Timer stopWatch = new Timer(1000, new ActionListener(){
        public void actionPerformed(ActionEvent event){
            timerDisplay.setText(TIME.format(count++ * 1000));
        }
    });
Let me know if this works out for you?

Thanks,

Yak
Yakisoba is offline   Reply With Quote
Old 10-16-2006, 08:42 AM   PM User | #10
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
With this code:

Code:
import java.util.*;
import java.text.*;

public class DateTest {
	
    private static final long START_TIME = 80492401;
    
	public static void main(String args[])
	{
		long count = START_TIME;
		SimpleDateFormat TIME = new SimpleDateFormat("HH:mm:ss");
		for(int i = 0; i < 10; i++)
		  System.out.println(TIME.format(count++ * 1000));
	}
	
}
I get the following output:

Quote:
11:00:01
11:00:02
11:00:03
11:00:04
11:00:05
11:00:06
11:00:07
11:00:08
11:00:09
11:00:10
I basically used your code and did a slight copy and paste..
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Old 10-24-2006, 05:26 AM   PM User | #11
Yakisoba
Regular Coder

 
Join Date: Feb 2005
Location: Tokyo, Japan
Posts: 151
Thanks: 0
Thanked 0 Times in 0 Posts
Yakisoba is an unknown quantity at this point
I replied to this a while ago, but I guess it didn't post...

Anyway, thanks a lot for looking into and testing my code. It appears that my random start time is specific to my current time zone (system time zone settings).

I was able to replicate your output by changing the Time zone.

I'll have to look into your previous suggestion and maybe create an instance of Calendar to get a more robust solution.

Thanks again,

Yak
Yakisoba is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:23 PM.


Advertisement
Log in to turn off these ads.