0

I am having a bit of trouble parsing a timestamp string brought back from the server using JSON. I am using the following code to extract the timestamp from a HashMap and display it in the Log:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Long time = Long.parseLong(event.get("time"));
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
String event_date = sdf.format(cal.getTime());
Log.v("Date", event_date);

But for every timestamp I pass into this method it brings back the date 16th Jan 1970, an example of a timestamp that I am trying to parse is 1336773600 which should correspond to the 11th May 2012. I can't see what I am doing wrong here but obviously I am. Any suggestions?

1
  • What is the event object you are using? Is it possible it is not, in fact, returning time in milliseconds? Commented May 9, 2012 at 15:18

2 Answers 2

8

Unix timestamps are seconds since the epoch. Java uses milliseconds since the epoch.

Multiply your unix timestamp by 1000 to adjust for this.

Sign up to request clarification or add additional context in comments.

Comments

2

try:

cal.setTimeInMillis(time * 1000);

to convert seconds to milliseconds.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.