简体   繁体   中英

How to generate 12 digit sequence number in java

I want to generate 12 digit mobile number. It's start with 910. Ex: 910000000001, 910000000002, 910000000003.

It is possible in java?

It depends on what you want to do.

If you want to just get the nextNumber: I would suggest AtomicLong. https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLong.html#AtomicLong-long-

class GetNextNumber {
    private static AtomicLong numberGenerator = new AtomicLong(910000000000L);

    public static long getNext() {
        return numberGenerator.getAndIncrement();
    }
}

If you need random numbers (with possible duplicated, you can use the ThreadLocalRandom as suggested by Ravi.

It seems like you want to generate all numbers in sequence from 910000000000 to 910999999999?

You could do something like this.

    long counter = 910000000000L;
    while(counter <= 910999999999L) // You can change this constant if you want the loop to break sooner
    {
        //Print, add to list, whatever
        counter++;
    }

Edit: It needs to be long

如果您正在寻找以 9 开​​头的随机数 12 位数字,您可以使用ThreadLocalRandom

System.out.println( ThreadLocalRandom.current().nextLong( 910000000001L, 910999999999L ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM