Java时间戳+随机数生成唯一数列

public class TestDate {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s=autoOrderId();
		System.out.print(s);
	}
	/**
     * 生成订单编号  时间戳+3位整数
     * @return
     */
    public static String autoOrderId(){

        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = format.format(date);
        Date orderId = null;
        Random rand = new Random();
        //[900]:900个    100:从100
        int x = rand.nextInt(900) + 100;
        try {
           orderId = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return String.valueOf(orderId.getTime()) + x;
    }


}

 

你可能感兴趣的:(Java)