• Post Reply Bookmark Topic Watch Topic
  • New Topic

calender class function getTimeInMillis() returns same values  RSS feed

 
Gamb Rockwell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the Calandar class in java.util and after calling two different instances of calender and printing the time in milliseconds I keep on getting the same value.

Calendar cal = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();


date1= cal.getTimeInMillis();
date2 = cal.getTimeInMillis();

System.out.println(date1+" "+date2);


I get the same value for date1 and date2.
 
Gamb Rockwell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I copied the wrong code, I made the changes in the line :
date2 = cal2.getTimeInMillis(); and not date2 = cal.getTimeInMillis(); as I had previously.

Calendar cal = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();


date1= cal.getTimeInMillis();
date2 = cal2.getTimeInMillis();

System.out.println(date1+" "+date2);
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They aren't ALWAYS equal...



Output:
C:\_Work\java\src>java MilliCheck
Using default
Try the date comparison this many times: 1000
1112299146777 1112299146787
1112299146787 1112299146797
1112299146807 1112299146817
1112299146817 1112299146827
1112299146827 1112299146837
1112299146837 1112299146847

C:\_Work\java\src>java MilliCheck
Using default
Try the date comparison this many times: 1000
1112299147748 1112299147758
1112299147778 1112299147788

C:\_Work\java\src>java MilliCheck
Using default
Try the date comparison this many times: 1000
1112299148890 1112299148900
1112299148900 1112299148910
 
Gamb Rockwell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a time sensitive app that needs to differentiatie events within seconds of each other so I need to guarantee that the time is unique up to the seconds level. Can I do this in java.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, try using System.currentTimeMillis() instead of the Calendar thing. I'm looking at the source in jdk 1.5 currentTimeMillis is native. If you're using jdk 1.5 check out a new method System.nanoTime().


It seems like you are complaining that your code isn't differentiating time down to the milli- and nano-second when your requirement is "seconds".

From System API:

public static long currentTimeMillis()Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

My system seems to be in units of 10 milliseconds.
[ March 31, 2005: Message edited by: Carol Enderlin ]
 
Gamb Rockwell
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the replys carol but what I want to do is to just differentiate based on time preferably in seconds but milli seconds would also do if I get it to work. I tried the System.currentTimeMillis() that you suggested and I get the same results(both times are the same). Is there anyway I could call two different statements right after the other and get two different times.

Thanks
 
Yevgeniy Treyvus
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The times are not guaranteed to be equal. This is because different statements/operations can take different amounts of time to execute. Some can take more than 1ms some can take less.

I am not quiet sure what you're trying to do in your application. But here are some possibilities that you can try. If you're issuing separate statements and want to make sure that they happen within 1ms (or 1sec) of each other, you can try to put


......

between the statements.

A better idea, though, might be to look into java.util.Timer class. Using this class you can try to schedule various events to happen at various times. So in your case you would use something like



method to execute task A every delay milliseconds.

[ March 31, 2005: Message edited by: Yevgeniy Treyvus ]
[ March 31, 2005: Message edited by: Yevgeniy Treyvus ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you make your unique value longer? Concatenate the milliseconds with a simple int incremented by 1 every time. You could get a few calls in the same millisecond, but not likely 2 billion.

I use a scheme that gets a 10 digit head from a database sequence number and increments a 3 digit tail. When the tail wraps over 999 it gets another head from the database. So I hit the database every thousand inserts.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!