com.luues.util.uuid.JUUID Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-util Show documentation
Show all versions of commons-util Show documentation
A Simple Tool Operations Class
package com.luues.util.uuid;
import com.luues.util.date.DateTime;
import com.luues.util.logs.LogUtil;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
public class JUUID {
private static NumberFormat nf = NumberFormat.getInstance();
private static AtomicInteger ai = new AtomicInteger();
// 32位
public static String getUUID() {
UUID uuid = UUID.randomUUID();
String uuidStr = uuid.toString().replaceAll("-", "");
return uuidStr;
}
public static String getShortUUID(){
//设置最大整数位数
nf.setMaximumIntegerDigits(3);
//设置最小整数位数
nf.setMinimumIntegerDigits(3);
if (ai.intValue() >= 999) {
ai.set(0);
}
return System.currentTimeMillis() + nf.format(ai.incrementAndGet());
}
// 16位
public static long generateUUID() {
if (ai.intValue() >= 999) {
ai.set(0);
}
return System.currentTimeMillis() * 1000 + ai.incrementAndGet();
}
public static void main(String[] args) {
long t1 = System.currentTimeMillis(); // 排序前取得当前时间
for (int i = 0; i < 1000000; i++) {
System.err.println(generateUUID());
}
long t2 = System.currentTimeMillis(); // 排序后取得当前时间
Calendar c1 = Calendar.getInstance();
c1.setTimeInMillis(t2 - t1);
System.out.println("generateUUID-耗时: " + c1.get(Calendar.MINUTE) + "分 "
+ c1.get(Calendar.SECOND) + "秒 " + c1.get(Calendar.MILLISECOND) + " 毫秒");
}
private static int sequence = 0;
private static int length = 6;
/**
* YYYYMMDDHHMMSS+6位自增长码(20位)
* @author shijing
* 2015年6月29日下午1:25:23
* @return
*/
public static synchronized String getLocalTrmSeqNum() {
sequence = sequence >= 999999 ? 1 : sequence + 1;
String datetime = DateTime.format("yyyyMMddHHmmss");
String s = Integer.toString(sequence);
return datetime +addLeftZero(s, length);
}
/**
* 左填0
* @author shijing
* 2015年6月29日下午1:24:32
* @param s
* @param length
* @return
*/
public static String addLeftZero(String s, int length) {
// StringBuilder sb=new StringBuilder();
int old = s.length();
if (length > old) {
char[] c = new char[length];
char[] x = s.toCharArray();
if (x.length > length) {
throw new IllegalArgumentException(
"Numeric value is larger than intended length: " + s
+ " LEN " + length);
}
int lim = c.length - x.length;
for (int i = 0; i < lim; i++) {
c[i] = '0';
}
System.arraycopy(x, 0, c, lim, x.length);
return new String(c);
}
return s.substring(0, length);
}
}