How to get the last 4 numbers of a variable?

2

I have to get last numbers from a variable and I do not know how to do it. I'm getting the milliseconds and I need to play this variable lol , where I need to return only the last 4 digits of lol not whole integer.

package trabalho01;

import java.util.Calendar;

public class aleatorio {

    public int aleatoriar() {       
        Calendar lCDateTime = Calendar.getInstance();
        int lol = (int) (lCDateTime.getTimeInMillis());     
        return lol;
    }
}
    
asked by anonymous 14.03.2015 / 18:36

1 answer

4

You can use the module operator to grab the rest of the 10 (our numeric base) raised to 4 (the digits you want to get), that is, 10000.

public int aleatoriar() {       
    Calendar lCDateTime = Calendar.getInstance();
    return (int)(lCDateTime.getTimeInMillis() % 10000);
}

See working on ideone .

The intermediate variable is unnecessary (when it is difficult to give a meaningful name to it, it is probably not useful).

    
14.03.2015 / 18:45