/** * 生成訂單的編號order_sn */ public static String generateOrderNumber() { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); String timeStr = DateUtils.format(cal.getTime(), "yyyyMMddHHmmssSSS"); return timeStr + CharUtil.getRandomNum(6); }
public class CharUtil{ /** * 獲取隨機字符串 * * @param num * @return */ public static String getRandomNum(Integer num) { String base = "0123456789"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < num; i++) { int number = random.nextInt(base.length()); sb.append(base.charAt(number)); } return sb.toString(); } }
--------------------------------------------------以上--------------------------------------------
1.charAt() 方法解釋:
用於返回指定索引處的字符。索引范圍為從 0 到 length() - 1。
public class Test { public static void main(String args[]) { String s = "www.runoob.com"; char result = s.charAt(8); System.out.println(result); } }
運行結果:
o
------------------------------------------------------------
2.nextInt()方法解釋:
不帶參數的nextInt()會生成所有有效的整數(包含正數,負數,0)
帶參的nextInt(int x)則會生成一個范圍在0~x(不包含X)內的任意正整數