Android - Not able to get microseconds using java.time.Instant
Asked Answered
C

0

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.

Campanula answered 10/4, 2020 at 12:53 Comment(13)
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?Javierjavler
can you try like this Instant.now().truncatedTo(ChronoUnit.NANOS); ?Absorption
Actually I required the microsecond precision which should be passed to the backend server in my project. What work around would you suggest @JavierjavlerCampanula
@Deadpool Yes, I tried it. But didn't get the microsecond precision - 2020-04-10T13:08:00.320ZCampanula
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.Javierjavler
I'm not sure what library you are using @Campanula you should wait for correct people to answerAbsorption
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)?Helio
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.Campanula
It seems, android studio doesn't correctly configured with java 9. So kindly help me to fix this issue.Campanula
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.Helio
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://mcmap.net/q/412005/-how-to-add-java-9-to-android-studio) that android doesn't support have 9. Is it true? So I would not be able to achieve this in android?Campanula
Instant.now() is pretty bad on accuracy. While Java 9 is said to implement a better clock, Android doesn't follow Java's release cycle or API implementation.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 testedTheorem
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.Metameric

© 2022 - 2024 — McMap. All rights reserved.