package net.mmgg.util;import java.io.File; import java.io.IOException; import java.text.DecimalFormat; import java.text.FieldPosition; import java.text.NumberFormat; import java.util.Random; import java.util.Stack; import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;
public class Identifier { private static final FieldPosition HELPER_POSITION = new FieldPosition(0); private static final long MAX = 999; private static final long FIX = 1388505600000l; private static long seq = 0; static final char[] array={'q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m','0','1','2','3','4','5','6','7','8','9','Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'};
public static String generate() { synchronized (Identifier.class) { StringBuffer sb = new StringBuffer(""); long datetime = System.currentTimeMillis() - FIX; sb.append(datetime); NumberFormat nf = new DecimalFormat("000"); nf.format(seq, sb, HELPER_POSITION); if (seq == MAX) { seq = 0; } else { seq++; } return sb.toString(); } } private static String _10_to_62(long number){ Long rest=number; Stack<Character> stack=new Stack<Character>(); StringBuilder result=new StringBuilder(0); while(rest!=0){ stack.add(array[new Long((rest-(rest/62)*62)).intValue()]); rest=rest/62; } for(;!stack.isEmpty();){ result.append(stack.pop()); } return result.toString();
}
public static String uid() {
return _10_to_62(Long.parseLong(generate()));
}public static String generateRadix() {
return Long.toString(Long.parseLong(generate()), Character.MAX_RADIX);
}
public static int getRandom(int min, int max) {
return new Random().nextInt(Math.abs(max - min)) + min;
}public static int get6Number() {
return getRandom(100000, 999999);
}
public static String uuid() {
return UUID.randomUUID().toString();
}public static long getIdtoDate(String id) {
long longId = Long.parseLong(id, Character.MAX_RADIX);
String ids = String.valueOf(longId);
ids = ids.substring(0, ids.length() - 3);
return Long.parseLong(ids) + FIX;
}}
Java生成唯一ID
未经允许不得转载:正能量 » Java生成唯一ID