3

I am trying to get microseconds from a Date, but I can't.

Date date = new Date()
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Salman Raza
  • 1,635
  • 6
  • 18
  • 18

3 Answers3

16

No, Date only stores values to millisecond accuracy. If you want microsecond accuracy, you might want to look at JSR-310, or java.sql.Timestamp as a bit of a hack - but don't expect any of the built-in classes such as Calendar and DateFormat to handle anything beyond milliseconds.

If you're trying to perform timing (e.g. for benchmarking) you should use System.nanoTime, but that should only be used for stopwatch-like use cases - it doesn't give you the "current wallclock time" in a meaningful way.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • You can see example following the link to API. – Artem Jan 06 '12 at 11:23
  • 3
    @SalmanRaza: It's hard to give an example when we don't know what you're trying to achieve. – Jon Skeet Jan 06 '12 at 11:36
  • i am trying to achive just the microsecond portion of this time "2012-01-03 12:50:49" in a seprate variable! – Salman Raza Jan 10 '12 at 05:31
  • @SalmanRaza: Well the microsecond portion of *that* time is just 0. It's still unclear what you're really trying to do - find the *current* time to microsecond accuracy, or read some *other* date/time to microsecond accuracy. If it's the latter, where is that time stored, and how? – Jon Skeet Jan 10 '12 at 07:10
  • @jon ok then how would i get the microsecond portion of the current time ? – Salman Raza Jan 10 '12 at 07:20
  • @SalmanRaza: You can't, generally - the system clock used in Java only reports time to the millisecond, which is generally more precise than the *real* accuracy anyway. You could investigate whether there's an NTP implementation somewhere and try to use that, I suppose. You really need to differentiate between whether your question is primarily about *representation* or *obtaining* data though - they're really different questions. – Jon Skeet Jan 10 '12 at 07:22
  • @Jon i dont know why are you not getting me .. . its simple that i want get current date time either by "Datetime" or "Calender" by any mean it possible but for that current time i want to get its microsecond at any cost how would i implement it in JAVA ?? – Salman Raza Jan 10 '12 at 07:29
  • 1
    @SalmanRaza: Neither `Date` nor `Calendar` provides that degree of precision, and nothing within Java will give you the current date/time to that degree of accuracy. `Timestamp` can *represent* a value down to nanoseconds, but normally that value is provided by a database. – Jon Skeet Jan 10 '12 at 07:33
2

What I do is find the difference between the currentTimeMillis and the nanoTime() at regular intervals (as this drifts quite a bit) and use nanoTime() + OFFSET for a currentTimeNS. (More specificity I use the RDTSC machine code instruction, which comes with even more quibbles ;)

However, its is less accurate than currenTimeMillis() over time which itself can drifts by many milli-seconds in an hour.

It only really useful if you understand its limitations.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

To measure time in microseconds you need to use the System.nanoTime() function which will return a time in microseconds based on the most accurate clock available to your system.

To the user this acts in pretty much the same way as System.currentTimeMilis() does for milliseconds, you simply define your start and end times and then negate the difference to find out how long an action took. For example:

long startTime = System.nanoTime();
//Do some kind of processing; 
//loop, IO, whatever you want to measure
long endTime = System.nanoTime();
long elapsedTime = endTime - startTime;

The last bit can be shorted, I've left it verbose for simplicity:

long elapsedTime = System.nanoTime() - startTime;

This should not be used to measure real time as it has very little meaning or guarantee that it is even comparable to it. All of the above information is available from the Java API documentation for System.

Turix
  • 178
  • 3
  • 9