美文网首页web开发帮助类
手动生成32位id的帮助类(二)

手动生成32位id的帮助类(二)

作者: 根艮哏艮根 | 来源:发表于2018-11-23 08:47 被阅读0次
    public class PrimarykeyUtil {
        
        private static int ext=100;
    
        public static synchronized String getPrimaryId(){
            SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String orderid = sdf.format(new Date());
            if(ext >999){
                ext = 100;
            } else {
                ext++;
            }
            String rand ="";
            for(int i=0;i<10;i++){
                Random rm = new Random();
                rand +=rm.nextInt(10);
            }
            return orderid + ext + rand;
        }
        
        
        public static synchronized String getPrimaryId(String prefix){
            SimpleDateFormat sdf =new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String orderid = sdf.format(new Date());
            if(ext >999){
                ext = 100;
            } else {
                ext++;
            }
            String rand ="";
            for(int i=0;i<10;i++){
                Random rm = new Random();
                rand +=rm.nextInt(10);
            }
            return prefix+"_"+orderid + ext + rand;
        }
        
        public static void main(String[] args) {
            System.out.println(getPrimaryId("menu"));
        }
        
        public static synchronized String getFileDirectory(){
            SimpleDateFormat formats =new SimpleDateFormat("yyyyMMddHHmmssSSS");
            return formats.format(new Date());
        }
        
        
        /**
         * 创建模板页面ID
         * @param templatePageID
         * @return
         */
        public static String createTemplatePageID(String templatePageID)
        {
            String newPageId = null;
            try {
                if(templatePageID != null){
                    String []temp = templatePageID.split("-");
                    DecimalFormat decimalFormat = new DecimalFormat("00");
                    long number = (Long) decimalFormat.parse(temp[1]);
                    number++;
                    newPageId = decimalFormat.format(number);
                }else{
                    newPageId = "01";
                }
            } catch (Exception e) {
                throw new  RuntimeException(e.getMessage());
            }
            return newPageId;
        }
        
    
    }
    
    
    

    相关文章

      网友评论

        本文标题:手动生成32位id的帮助类(二)

        本文链接:https://www.haomeiwen.com/subject/wrudqqtx.html