zoukankan      html  css  js  c++  java
  • java-生成id的方法(时间+随机数)

     1 package com.dawning.gridview.app.infrastructure.webapp.devicecontrol.global;
     2 
     3 import java.text.SimpleDateFormat;
     4 import java.util.Date;
     5 
     6 /**
     7  * 生成id
     8  * @author wjy
     9  *
    10  */
    11 public class GeneratID {
    12     /**
    13     * 根据传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
    14     *
    15     * @param sformat
    16     *            yyyyMMddhhmmss
    17     * @return
    18     */
    19     public static String getDate(String sformat) {
    20        Date currentTime = new Date();
    21        SimpleDateFormat formatter = new SimpleDateFormat(sformat);
    22        String dateString = formatter.format(currentTime);
    23        return dateString;
    24     }
    25 
    26     public static String getRandomNum(int num){
    27         String numStr = "";
    28         for(int i = 0; i < num; i++){
    29             numStr += (int)(10*(Math.random()));
    30         }
    31         return numStr;
    32     }
    33     /**
    34      * 生成id
    35      * @return
    36      */
    37     public static Long getGeneratID(){
    38         String sformat = "MMddhhmmssSSS";
    39         int num = 3;
    40         String idStr = getDate(sformat) + getRandomNum(num);
    41         Long id = Long.valueOf(idStr);
    42         return id;
    43     }
    44 
    45     public static void main(String[] args) {
    46         for(int i = 0; i < 1; i++){
    47             System.out.println(getGeneratID());
    48         }
    49     }
    50 }

    转自同事的方法;

  • 相关阅读:
    游标、锁
    树形背包浅谈
    金字塔
    Codeforces Round #652 (Div. 2) 题解
    NOI1999 棋盘分割
    NEERC2002 Folding
    HDU4283 You Are the One
    Codeforces Round #646 (Div. 2) 题解
    洛谷 P1679 神奇的四次方数
    UVA12563 劲歌金曲
  • 原文地址:https://www.cnblogs.com/My-Cloud/p/4546317.html
Copyright © 2011-2022 走看看