user836200
user836200

Reputation: 655

Converting Unix Date from php to Java (Android)

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?

Upvotes: 0

Views: 792

Answers (1)

Frank Fang
Frank Fang

Reputation: 1109

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)));

Upvotes: 2

Related Questions