• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get microseconds in current date in Java/Oracle?  RSS feed

 
S Shiva
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a requirement from client that current date is required upto microseconds(YYYYMMDDHHMIssSSS) in a JSP Form...
I am using Windows xp professional...
I tried it using java as well as oracle(YYYYMMDDHHMISSFF) and the best I could come up with is milliseconds...
The microsecond part is always 000 both in java as well as oracle...
I used CURRENT_TIMESTAMP in oracle which could give me 9 precision digits...but it only gives milliseconds rest are 0's...

I think I read somewhere windows doesnot give precision beyond millisec..
On linux systems you can get nano using Oracle... Is it true??? or Is there a way to get milliseconds in java???
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well Oracle gives you up to 9 significant figures for milliseconds (FF9) right? If necessary, use this to convert to microseconds.

And I'm afraid Oracle itself doesn't print of microseconds separately.

Similarly, if you want to do it Java. Given your return Oracle type is java.sql.Timestamp then there is a getNano(). Again some conversion is needed to get microsecond.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Shiva wrote:There is a requirement from client that current date is required upto microseconds(YYYYMMDDHHMIssSSS) in a JSP Form...

Then my suggestion would be to simply add "000" to the end of a standard string.

The fact is that even the nanosecond timer (which is NOT clock based, and is quite a bit slower) may not even be accurate to microseconds; and the standard timer probably isn't accurate to milliseconds either, so you can take the last couple of digits of that with a pinch of salt.

So, my advice is: Tell your client that you can certainly return the format they want; but they'd better not rely on the accuracy of it.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a difference between "precision" and "accuracy". You can get the former but not the latter.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Shiva wrote:There is a requirement from client that current date is required upto microseconds(YYYYMMDDHHMIssSSS) in a JSP Form...

I've done a bit of searching, because this question interested me too.

My conclusion: Your clients don't know what they're asking for or, perhaps more importantly - WHY what they're asking for is probably not possible - IF what they want is an accurate microsecond clock time.

CPU clock time, for many years (most of the 90's and early 00's) was measured in MegaHertz, and computers had (and have) very accurate internal clocks to keep track of cycle time; however, most Unix systems measured time in 'tics' (1/100th of a second) - and even they were known to be inaccurate. Now, we deal with GigaHertz chips, but I doubt whether things are significantly different.

Why? I'm not exactly sure, but I'd care to bet that it's related to the Heisenberg Principle.

Simply put: the act of measuring computer time, no matter how accurately it's recorded, takes time; and the accuracy of any measurement is much more closely related to the number of times you can do it in a unit of external time (eg, 1 second) than the speed of the chip or CPU involved.

And since Java is orders of magnitude removed from older languages that were closer to the operating system, the likelihood of getting an accurate microsecond time - and don't forget that means that we need to be able to call whatever "measurement" method there is 1 million times a second and know that the result will be accurate - seems remote.

System.nanosecond() gives no guarantees of accuracy; it simply says that it will use the "best available clock".

If your clients just want it for display: Fine - do what I suggested above.
If they are going to rely on what they get, tell them that they need to invest in proper computer-based microsecond-accurate timing devices - probably NTP-based, but I honestly don't know - and also that they're going to need to trust your company to install at least one of them on your system specifically for them.

HIH

Winston
 
S Shiva
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all..
The thing is, the current date is going to be used to create an application no. which is bound to be 20 digits and they do not want any 0's..
I'll try subtracting system time in nano from milli and add it to current time. No body is ever going to know whether the microsecond part is accurate...
So I'll try it and revert back...
Thanks..
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Shiva wrote:The thing is, the current date is going to be used to create an application no. which is bound to be 20 digits and they do not want any 0's..
I'll try subtracting system time in nano from milli and add it to current time. No body is ever going to know whether the microsecond part is accurate...

Well, if you're going to do that, my suggestion would be to use a cycling sequence number from 0 to 999 and just add it to the end of a millis date/time.
For one thing it'll be a lot quicker; and it pretty much eliminates any chance of a collision. The only thing to remember is that if several threads can be creating application numbers at the same time you'd better make sure it's synchronized.

you should also make sure your client knows what you're doing and why.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!