生成订单号
/** * 生成订单的编号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)内的任意正整数