• Post Reply Bookmark Topic Watch Topic
  • New Topic

Format time in miliseconds using SimpleDateFormatter  RSS feed

 
thakur thakur
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have format time in milliseconds in a particular format(precise to two digits). I am using following code:
Timestamp ts = new Timestamp(System.currentTimeMillis());
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss:SS aaa");
return formatter.format(ts);
Ran this loop
for(int i=0;i<4;i++){
Thread.sleep(1000);
System.out.println(getREF_DTM());
}
The caveat is, for milliseconds it always returns 00.
I would appreciate any help.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TimeStamp doc says getTime() returns integral seconds. Must be lopping off the millis. This shows millis:

It also shows (on my PC) that no matter what the millis say at start, they only increment by 10's. Hardware clock resolution or something in Windows?
 
thakur thakur
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your quick reply. I tried the following and I get the same milli seconds for some runs. It is not consistent. The problem is I have to make sure that I generate different milli seconds. As you pointed out, I am trying it on Windows might have to try on AIX.
SimpleDateFormat lFormat = new SimpleDateFormat( "MM/dd/yyyy hh:mm:ss:SSS aaa" );
long lMillis = System.currentTimeMillis();
Date lDate = new Date(lMillis);
return lFormat.format( lDate );
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is I have to make sure that I generate different milli seconds.
Well the first thing to note is that if you want to show milliseconds, you should probably be displaying three digits, rather than two.
Second, why shouldn't milliseconds be the same each time? You're asking for a 1000 ms delat, then waking up. If the timing is exact enough, you'll get the same value formilliseconds every time. Why is this a surprise, or a problem? In practice the value will gradually increase over time, as there will be additional delays beyond the 1000 milliseconds (since running code takes time too) and as these accumulate, the milliseconds value will gradually increase.
But seriously - why do you want millisecons to stay the same? Try replacing sleep(1000) with sleep(1013) (or just about any other number) if you like, to see what that does. How are these results in any way different from what we should expect?
[ July 28, 2003: Message edited by: Jim Yingst ]
 
thakur thakur
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr sheriff,
If you look into the code snippet I posted the second time, I removed the sleep and this is not about displaying any thing it's about storing in the database and the string is a part of the Primary Key.
I guess the answer is If I generate more than one request in less than one milli second, the string gonna be same..... It shouldn't matter what OS I am using.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the answer is If I generate more than one request in less than one milli second, the string gonna be same.....
Yup. For that matter if the requests are closer than 10 ms you'll probably have the same problem.
It shouldn't matter what OS I am using.
Well, some OS's and some hardwares do not have any way to measure millisconds to better than 10 ms resolution. There's really no "platform neutral" way to fix this. If you want a completely platform-independant API, they could just refuse to tell you the time at all, but that's not really desireable for most of us.
If you want to generate a unique primary key, a timestamp is no a very reliable way to ensure it. I'd recommend using an auto-incrementing counter of some sort.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by thakur thakur:
Mr sheriff,
it's about storing in the database and the string is a part of the Primary Key.

Either you need to call sleep (x), to get different timestamps, or change the way of generating primary keys in a different way (automatically, for instance).
An operating system, which is slow enough, to generate distinct values today might be to fast tomorrow, running on faster hardware.
 
thakur thakur
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I agree with you. I removed the dependency by going to sleep for a sec (I now I can do 10ms). Didn't want to take any chances.. The application is not a real time one and can live with one sec latency for every request.
Enjoyed the session guys. Thanks for all the help I had.
Regards.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!