1

I am unable to get the microseconds using java.time.Instant (Instant.now()) from android.

I am getting the date time without microseconds like 2020-04-10T12:43:40.442Z from android

But when I execute a independent java program, with the use of java.time.Instant (Instant.now())

I am getting the date time with microseconds like 2020-04-10T12:43:40.442179Z

It seems the issue is with java versions in android studio. Please help me to fix this issue.

Note:

I have installed Android Studio - 3.6.1 and done the JDK 9(9.0.4) Setup in the Android studio Settings.

SKK
  • 1,705
  • 3
  • 28
  • 50
  • 1
    The availability of microsecond precision depends on the clock source that the libraries on the device happen to use. Why do you need it - can you work around not having it? – Joni Apr 10 '20 at 13:02
  • can you try like this `Instant.now().truncatedTo(ChronoUnit.NANOS);` ? – Ryuzaki L Apr 10 '20 at 13:03
  • Actually I required the microsecond precision which should be passed to the backend server in my project. What work around would you suggest @Joni – SKK Apr 10 '20 at 13:07
  • @Deadpool Yes, I tried it. But didn't get the microsecond precision - 2020-04-10T13:08:00.320Z – SKK Apr 10 '20 at 13:10
  • 1
    My first suggestion is to fake it. If constant 0 does not work, use a counter for example. My second suggestion is to look for external libraries. It might be possible to get a more precise time value through native code, and it's possible someone has already wrapped up a solution in a nice library. – Joni Apr 10 '20 at 13:11
  • I'm not sure what library you are using @SKK you should wait for correct people to answer – Ryuzaki L Apr 10 '20 at 13:11
  • Is that an issue in Android Studio, on your Android device or both? Which independent Java program does better (or appears to do, it's probably faking it)? – Ole V.V. Apr 10 '20 at 16:19
  • I have created independent java program then compiled and executed in java 9 and it returns the microsecond precision. But when I compile and execute the same java program in java 8, it doesn't return the microsecond precision. So I identify that the issue is because of Java version. – SKK Apr 10 '20 at 17:41
  • It seems, android studio doesn't correctly configured with java 9. So kindly help me to fix this issue. – SKK Apr 10 '20 at 17:42
  • I think it is more than a configuration issue. The precision of `Instant.now()` since Java 9 depends on the platform and the integration between the JVM and the platform. So I'd be afraid that you may not get better with Android Studio, or not with that version of Android Studio on that hardware and operating system. – Ole V.V. Apr 10 '20 at 17:57
  • I am using android studio 3.6.2. I want to know, whether android does support Java 9. Because, I do see in some other thread(https://stackoverflow.com/a/58516755/1584121) that android doesn't support have 9. Is it true? So I would not be able to achieve this in android? – SKK Apr 13 '20 at 07:15
  • Instant.now() is [pretty bad on accuracy](https://stackoverflow.com/a/20689231/6296561). While [Java 9 is said to implement a better clock](https://stackoverflow.com/a/33478391/6296561), Android [doesn't follow Java's release cycle or API implementation](https://stackoverflow.com/q/47230781/6296561).What you can do with Java may or may not be possible to do with Android, for that reason. Tbf, while Android has been sued for copying code from Oracle, far from all implementations are the same. The first post I linked contains a possible workaround with nanosecond resolution, but I haven't tested – Zoe Apr 13 '20 at 10:17
  • 1
    The precision of the clock is not a language feature. The runtime implementation that ships with Oracle’s JDK 9 happens to include support for higher precision, but that doesn’t imply that every Java 9 environment must have a high precision clock. Neither does the opposite apply. Other Java 8 implementations are not required to have no high precision clock, just because Oracle’s implementation of Java 8 has no high precision clock. In other words, on Android you get Android’s clock implementation, regardless of which language version you use for writing your code. – Holger Apr 15 '20 at 15:24

0 Answers0