user1106130
user1106130

Reputation: 301

Adding 1,15,30 or 45 minutes to currentTimeMillis()

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

Answers (2)

David Schwartz
David Schwartz

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

paxdiablo
paxdiablo

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

Related Questions