1. 程式人生 > >訂單號生成方法

訂單號生成方法


package com.jae.impl;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
@Service
public class OrderServiceBean extends DaoSupport<Order> implements OrderService{
    
     * 生成訂單號,訂單號的組成 兩位年+兩位月+兩位日+兩位小時+當天的訂單總數 如:11071012100
     * @param createDate
     * @return
     */
    private String buildOrderid(Date createDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHH");
        StringBuilder out  = new StringBuilder(dateFormat.format(createDate));
        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String strdate = dateFormat.format(createDate);// 2011-07-10
        Date date = createDate;
        try {
            date = dateFormat.parse(strdate);
        } catch (ParseException e) {e.printStackTrace();}
        long count = (Long)em.createQuery("select count(o) from Order o where o.createDate>=?1")
        .setParameter(1, date, TemporalType.TIMESTAMP).getSingleResult();
        out.append(count+1);
        return out.toString();
    }
}