0

In my android app I'm creating a timestamp this way:

final BackupInfo backupInfo = new BackupInfo(description, System.currentTimeMillis(), backupContacts.size());

eg, using System.currentTimeMillis()

Now I convert it back to date format using:

public static String getDate(long time) 
{
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time);
    String date = DateFormat.format("dd-MM-yyyy HH:mm:ss", cal).toString();
    return date;
}

And it works fine.

But now I'm receiving a timestamp from a server and the date String I receive from getDate is not the correct date.

Practical case:

My app generates this timestamp: 1403022230766

getDate returns this date: 17-06-2014 05:23:50 which is correct to my eyes.

Now the problem comes in, I get this timestamp from the server: 1403022360

getdate returns this date: 16-01-1970 18:43:42 which is totally wrong, it should be close to the timestamp generated by my app.

The timestamp returned by the server is 3 digits less in size. But if I go to an online converter, like this one and I put 1403022360 (the TS generated by the server) I get a correct date.

Can anyone explain me why this difference and what am I doing wrong in my getDate method that I can't decode the timestamp received from the server?

dazito
  • 7,740
  • 15
  • 75
  • 117

1 Answers1

3

Your server is returning your timestamp in seconds, so multiply by 1000 to get milliseconds.

The online converters work properly because they assume that the that if the number is large enough, then it is in milliseconds and if it's short then it is in seconds.

Java/Android dates are all long types so they can hold milliseconds for additional precision.

Uxonith
  • 1,602
  • 1
  • 13
  • 16
  • Yup, you are correct! It's working, I'll accept you answer as soon as the minimum time passes :) Thanks! – dazito Jun 17 '14 at 17:26