在电商项目中,图片名、商品ID都要唯一且方便存储,于是记录下这两个ID生成策略的方法,以便日后项目再有需要。具体代码如下

  import java.util.Random;

  /**
     * 图片名生成
     */
    public static String genImageName() {
        //取当前时间的长整形值包含毫秒
        long millis = System.currentTimeMillis();//加上三位随机数
        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();//加上两位随机数
        Random random = new Random();
        int end2 = random.nextInt(99);
        //如果不足两位前面补0
        String str = millis + String.format("%02d", end2);
        long id = new Long(str);
        return id;
    }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-21
  • 2021-09-20
  • 2022-12-23
  • 2021-12-18
  • 2021-11-15
猜你喜欢
  • 2021-09-19
  • 2021-07-21
  • 2022-12-23
  • 2021-08-21
  • 2021-12-25
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案