码迷,mamicode.com
首页 > 其他好文 > 详细

032-IDUtils 工具类模板

时间:2018-06-13 15:19:39      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:time()   rip   mil   int   ret   orm   public   java   end   

模板一:

package cn.e3mall.common.utils;

import java.util.Random;

/**
 * 各种id生成策略
 * @title:IDUtils
 * @description:
 * @author jepson
 * @date 2018年6月13日 下午12:18:25
 * @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());
    }
}

 

032-IDUtils 工具类模板

标签:time()   rip   mil   int   ret   orm   public   java   end   

原文地址:https://www.cnblogs.com/jepson6669/p/9177134.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!