• Post Reply Bookmark Topic Watch Topic
  • New Topic

any one know how to change a long to an int?  RSS feed

 
Andrew Brown
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
having problems changing a long from a System.currentTimeMillis(); in to an integer.

(It smaller than the time as it's the difference between two times so no worries on the size restrictions)

Cheers
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you want to convert this one because definately

System.currentTimeInMillis will gave a large number which might be not

comfortably suited in int..

That will might be loose the data you really want....

otherwise still if you want to convert the long into Int the Altenative way

is::




This is how a long can be converted to int variable...

But still it will be better if you eloborate your question some more... so that ranchers will guide you the alternative way......
 
Jass Singh
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur: Andrew has already told that value is a difference between times and hence a small value-

Originally posted by Andrew Brown:

(It smaller than the time as it's the difference between two times so no worries on the size restrictions)

Cheers



Andrew: Approach shown by Ankur is perfectly fine.

Regards,
Jass
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jass Singh:
Ankur: Andrew has already told that value is a difference between times and hence a small value


Just a quick point. It is essential that you do the subtraction of the times using longs, then cast the difference to int. If you cast the two times to int then subtract them, all sorts of intermittent problems would occur.
 
Andrew Brown
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your help,

it worked a treat!
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried it out and got some errors. I think, if the difference is less than Integer.MAX_VALUE, you will get the correct answer, but if the difference is more than Integer.MAX_VALUE, you will get overflow problems.
Casting the values to (int) before the subtraction doesn't seem to make any difference.

CR
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!