3

http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html

The only non-deprecated ctor takes millis. Is there no way to ask for 'now' including nanos.

Dev
  • 11,919
  • 3
  • 40
  • 53
bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • Sorry, I don't understand your question. I see the method [`getNanos`](http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html#getNanos%28%29) here, so I'm confused as to what you're asking for. Could you please clarify what's wrong? – blahman Jan 31 '12 at 02:11
  • Your answer seems to indicate that you understand my question. – bmargulies Jan 31 '12 at 02:50
  • Haha, sorry, I read some of the answers and realised what a ctor was, then started reading and posted an answer. Sorry about the confusion ^^' – blahman Jan 31 '12 at 02:52

2 Answers2

4

getNanos()/setNanos(int) on Timestamp exists to support databases that return the timestamp data type with nanosecond precision.

Java does not have a means to get the current time with nanosecond precision. System.nanoTime() returns elapsed nanoseconds from some unknown epoch chosen by the JVM. It is useful for measuring elapsed time between two calls of System.nanoTime(). Even then while System.nanoTime() can measure elapsed time between calls to nanosecond precision it is not necessarily accurate to nanosecond precision as mentioned in the javadocs.

Dev
  • 11,919
  • 3
  • 40
  • 53
3

As has been noted in comments on other answers, System.nanoTime() doesn't really return any meaningful values regarding time in nanoseconds.

I would like to point to this discussion from SO which points out why we can't get better accuracy on our time measurements. It would appear to imply that it is not really possible to do as you ask, and that it would be against some Java design goals to try and include this ability, due to portability and platform constraints.

So, in answer to your question, I would say no, but only based upon the answers of people with more rep than me.

Community
  • 1
  • 1
blahman
  • 1,304
  • 1
  • 12
  • 18