-3

Hi all i am trying to store android current timestamp value in a string variable without losing its precision but i am losing last 3 digits when i try to print timeStamps value. Actually its having 13 digits but only 10 digits getting printed.

String timeStamp = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));

I got the above snippet from this answer I am in the process to get the recently updated contacts based on CONTACT_LAST_UPDATED_TIMESTAMP for which precision seems important.Please guide me

Jose Kj
  • 2,298
  • 1
  • 22
  • 33
  • 1
    I bet you are losing the 3 millisenconds digits. That may be due to a conversion to seconds... maybe here: `toSeconds(System.currentTimeMillis())` – jhamon Aug 25 '19 at 12:13
  • 2
    You are converting it to a whole number of seconds. The string has nothing to do with the 3 digits of lost precision. – Andy Turner Aug 25 '19 at 12:13
  • how to fix this,please guide me – Jose Kj Aug 25 '19 at 12:13
  • 2
    Easy: don't convert it to seconds. Don't just copy/paste code but try to read the complete cited answer and try to understand what you copy – jhamon Aug 25 '19 at 12:14
  • ok thanks @jhamon i shall try this... String.valueOf(TimeUnit.MILLISECONDS(System.currentTimeMillis())); – Jose Kj Aug 25 '19 at 12:16
  • 1
    No :) :) Try to understand *each* peace of your code. Just `String.valueOf(System.currentTimeMillis());` – mentallurg Aug 25 '19 at 12:18

1 Answers1

1

The documentation says that CONTACT_LAST_UPDATED_TIMESTAMP is:

Timestamp (milliseconds since epoch) of when this contact was last updated

System.currentTimeMillis() returns the milliseconds since epoch representing the current time.

Your primary problem is that you are converting milliseconds to seconds, which is unnecessary and incorrect for this use case.

Your secondary problem is that you are converting the value to a String, which AFAIK should not be necessary for this particular column.

So, just use System.currentTimeMillis(), without any conversions.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253