I have a table which stores both unix time and the equivalent time stamp.
CREATE TABLE tbl_time
(
time_unix BIGINT,
time_timestamp TIMESTAMP WITHOUT TIME ZONE
);
The database is in PostgreSQL. database has been configured with Asia/Tehran time zone.
for example:
1333436817, 2012-04-03 11:36:57
When I convert the unix time into string format in python with:
datetime.datetime.fromtimestamp(1333436817)
it gives me: datetime.datetime(2012, 4, 3, 11, 36, 57) which is correct and equal to database. But when I do this conversion with java using:
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Asia/Tehran"));
c.setTimeInMillis(1333436817 * 1000);
System.out.println(c.getTime());
It gives: Sat Jan 24 06:12:35 IRST 1970. The system itself is running under Asia/Tehran time zone. I’m using PostgreSQL 8.4.11 on Debian 6.0.5 with python 3.1 and openjdk 6. Can anyone help?
The result of 1333436817 * 1000 is too big for an integer so it overflows. Java will not promote the type for you automatically in this case.
Try that:
Notice the
Lthat force your calculation to use long integer.