Converting long to int in Java Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Converting long to int in Java without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m using System.currentTimeMillis() for getting seconds since the epoch. This is an example.

 long enable_beacon_timestamp = System.currentTimeMillis()/1000;

And the output gives:


So the problem is that there is a mismatch in cast value. What I want is to get the first output the same as the integer.

Can you provide some background why this happen?.


Your cast syntax is incorrect. You need also be aware that longs can be much bigger that the max value for int.

int y;
if ( enable_beacon_timestamp > (long)Integer.MAX_VALUE ) {
    // long is too big to convert, throw an exception or something useful
else {
    y = (int)enable_beacon_timestamp;

Try something like this perhaps…

We are here to answer your question about Converting long to int in Java - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji