zoukankan      html  css  js  c++  java
  • IDUtils (ID生成策略)

    package com.taotao.utils;
    
    import java.util.Random;
    
    /**
     * 各种id生成策略
     * <p>Title: IDUtils</p>
     * <p>Description: </p>
     * <p>Company: www.itcast.com</p> 
     * @version 1.0
     */
    public class IDUtils {
    
        /**
         * 图片名生成
         */
        public static String genImageName() {
            //取当前时间的长整形值包含毫秒
            long millis = System.currentTimeMillis();
            //long millis = System.nanoTime();
            //加上三位随机数
            Random random = new Random();
            int end3 = random.nextInt(999);
            //如果不足三位前面补0
            String str = millis + String.format("%03d", end3);
            
            return str;
        }
        
        /**
         * 商品id生成
         */
        public static long genItemId() {
            //取当前时间的长整形值包含毫秒
            long millis = System.currentTimeMillis();
            //long millis = System.nanoTime();
            //加上两位随机数
            Random random = new Random();
            int end2 = random.nextInt(99);
            //如果不足两位前面补0
            String str = millis + String.format("%02d", end2);
            long id = new Long(str);
            return id;
        }
        
        public static void main(String[] args) {
            for(int i=0;i< 100;i++)
            System.out.println(genItemId());
        }
    }
  • 相关阅读:
    svn备份
    Java Web开发引用包的方法
    nucht1.2二次开发增量采集
    程序集信息设置.net
    lucene的基本查询及lucene3.0.1API
    Asp.net网站部署
    C语言面试算法题(一)
    面试题
    C语言面试算法题(二)
    C++的IO流的函数
  • 原文地址:https://www.cnblogs.com/a-fun/p/9359946.html
Copyright © 2011-2022 走看看