Reputation: 301
does anyone know how to add minutes to currentTimeMillis. i am passing an integer t, which is how many minutes users would like to add to the currentTimeMillis() time_int. Is this possible in java? can anyone show me?
public void radioStartTime(int t)
{
time_int =(int)System.currentTimeMillis(); //casting long into int.
System.out.println(time_int);
}
Upvotes: 1
Views: 5941
Reputation: 182819
Your code can't possibly work. An int
can only hold up to (2^31)-1
. If you use it to hold milliseconds, that's only about 24 days from January 1, 1970 -- so you can't hold the current time. You either need to use a long
, or divide by 1,000 and store the time in seconds.
Upvotes: 2
Reputation: 882028
Well, given that there are 1,000 milliseconds in a second and 60 seconds in a minute, you need to add 60,000 for each minute.
Hence:
Add n minutes, n = Add
1 60,000
15 900,000
30 1,800,000
45 2,700,000
I'd also be a little wary of converting the return value from currentTimeMillis()
back to an int
data type, you may want to keep it as a long
so there's less chance of losing range.
Upvotes: 11