[java]代码库
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
public class UUIDGenerator {
private static AtomicInteger counter = new AtomicInteger(0);
public static String getUUID() {
UUID uuid = UUID.randomUUID();
String str = uuid.toString();
// 去掉"-"符号
String temp = str.substring(0, 8) + str.substring(9, 13) +
str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
return temp;
}
/**
* 长生消息id
*/
public static Long getAtomicCounter() {
if (counter.get() > 999999) {
counter.set(1);
}
Long time = System.currentTimeMillis();
Long returnValue = (time * 2394) + counter.incrementAndGet();
return returnValue;
}
private static long incrementAndGet() {
return counter.incrementAndGet();
}
public static void main(String[] args) {
System.out.println(getUUID());
System.out.println(getAtomicCounter());
}
}
by: 发表于:2018-05-24 10:40:21 顶(0) | 踩(0) 回复
??
回复评论