This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes any one know how to change a long to an int? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "any one know how to change a long to an int?" Watch "any one know how to change a long to an int?" New topic
Author

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

Andrew Brown
Greenhorn

Joined: Apr 10, 2006
Posts: 25
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


Thanks for any help anyone can offer.
Ankur Sharma
Ranch Hand

Joined: Dec 27, 2005
Posts: 1234
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

Joined: Mar 30, 2006
Posts: 52
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Andrew Brown
Greenhorn

Joined: Apr 10, 2006
Posts: 25
thank you for your help,

it worked a treat!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32708
    
    4
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: any one know how to change a long to an int?
 
Similar Threads
Tech Word Game
array store exception
Integer data type
WA #2 ..... word association
And he stop.....