Search code examples
javacastingprimitive

How to get a set of unique Integers if I have a set of unique Longs?


I hava a set of Longs (Timestamps actually). Apparently they can't be casted to Integers without loosing data because the timestamp doesn't fit Integer range. I need to generate a Primary Key of Integer type based on these Longs. Is there any way to do this?


Solution

  • Suppose the range of date timestamps are created is limited. Let's say dates after December, 12 2004. Seems like a reasonable approach truncating the highest significant digits, e.g.

    long 00001357288262
    int      1357288262