看註釋——複製即用
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class OrderUtils {
private static final String ORDER_CODE = "1";
private static final String RETURN_ORDER = "2";
private static final String REFUND_ORDER = "3";
private static final int[] r = new int[]{7, 9, 6, 2, 8, 1, 3, 0, 5, 4};
private static final int maxLength = 14;
private static String toCode(Integer userId) {
String idStr = userId.toString();
StringBuilder idsbs = new StringBuilder();
for (int i = idStr.length() - 1; i >= 0; i--) {
idsbs.append(r[idStr.charAt(i) - '0']);
}
return idsbs.append(getRandom(maxLength - idStr.length())).toString();
}
private static String getDateTime() {
DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return ""+System.currentTimeMillis();
}
private static long getRandom(long n) {
long min = 1, max = 9;
for (int i = 1; i < n; i++) {
min *= 10;
max *= 10;
}
long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min;
return rangeLong;
}
private static synchronized String getCode(Integer userId) {
userId = userId == null ? 10000 : userId;
return getDateTime() + toCode(userId);
}
public static String getOrderCode(Integer userId) {
return ORDER_CODE + getCode(userId);
}
public static String getReturnCode(Integer userId) {
return RETURN_ORDER + getCode(userId);
}
public static String getRefundCode(Integer userId) {
return REFUND_ORDER + getCode(userId);
}
}