Question

I need to convert Java long datatype (64-bit) data into legacy c++ app unsigned int (32-bit) datatype.

No worries about the data loss as the data is Linux timestamp, which would take aeons to hit unsigned int limit.

Any idea what transformation to apply to these numbers?

Thanks in advance!

P.S. - data-types example:

Java - 1266336527340

C++ - 1266336583

They both produce same date, and about same time (+/- a minute).

Was it helpful?

Solution

Java's Date.getTime returns the number of milliseconds from the epoch, whereas the C++ code expects the number of seconds from the epoch, so you need to divide by 1000 then truncate:

int timestampAsInt = (int)(timestampAsLong / 1000);

Java has only a signed integer not unsigned, but this should work.

OTHER TIPS

long javaTime = someDate.getTime();
int cTime = (int)((javaTime + 500) / 1000);

I prefer rounding over truncation, but you will have to decide which is right for your business rules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top