0

I'm using the following lines of code to convert the unix time I'm getting back from a php file but the dates are coming up incorrectly:

int unixTime = new Integer( inputjson[2].getString((Integer) x.get(j)) ).intValue(); 
long timestamp = unixTime * 1000;  // msec  
java.util.Date d = new java.util.Date(timestamp); 

Here's an excerpt from the php file:

date_default_timezone_set('America/New_York');
while($row1 = mysql_fetch_array($result1)) {
   $output1[]=$row1['text'];
   $temp = $row1['dateOfStatus']; 
   $d = strtotime($temp);
   $output5[] = $d; 
}

I've confirmed that the dateOfStatus field in the database is correct. However, when I get this value back in java and do the conversion, the dates are off: i.e., 11:11pm today returns: TUE Jan 06 05:41:12 EST 1970.

I'm not sure what's going wrong.

Is there anyone familiar with this issue?

1 Answer 1

2

This code is wrong:

long timestamp = unixTime * 1000;

unixTime is an integer with 10 digits, when multiply with 1000, the result is exceed max integer, so unixTime * 1000 get wrong result. You should convert unixTime to long, just like this:

long unixTime = Long.parseLong(inputjson[2].getString((Integer) x.get(j)));
Sign up to request clarification or add additional context in comments.

2 Comments

This code seems to fix the date but the timestamp is set to 0. Do you know why this happens?
Sorry, that's my fault! Thank you! This works great and totally makes sense!

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.