随笔 - 144  文章 - 0  评论 - 2  阅读 - 91716

根据时间获取单号

复制代码
public static String getCode(){
        String uuId="";
        Calendar now = Calendar.getInstance();
        System.out.println("年: " + now.get(Calendar.YEAR));
        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
        System.out.println("分: " + now.get(Calendar.MINUTE));
        System.out.println("秒: " + now.get(Calendar.SECOND));
        System.out.println("当前时间毫秒数:" + now.getTimeInMillis());
        System.out.println(now.getTime());
        uuId=String.valueOf(now.get(Calendar.YEAR))+String.valueOf(((now.get(Calendar.MONTH) + 1)))
        +String.valueOf(now.get(Calendar.DAY_OF_MONTH))+String.valueOf(now.get(Calendar.HOUR_OF_DAY))
        +String.valueOf(now.get(Calendar.MINUTE))+String.valueOf(now.get(Calendar.SECOND))
        +String.valueOf(getRandom(6));
        String current_time=getFomartDate("yyyyMMddHHmmss");
        uuId=current_time+String.valueOf(getRandom(6));
        return uuId;
    }
    /**
     * 生成固定长度随机码
     * @param n 长度
     */
    private static long getRandom(long n) {
        long min = 1, max = 9;
        for (int i = 1; i < n; i++) {
            min *= 10;
            max *= 10;
        }
        long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min;
        return rangeLong;
    }
    public static String getFomartDate(String format){
        return new SimpleDateFormat(format).format(new Date());
    }
    public static void main(String[] args) throws ParseException {
        String uuId="";
        Calendar now = Calendar.getInstance();
        System.out.println("年: " + now.get(Calendar.YEAR));
        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
        System.out.println("分: " + now.get(Calendar.MINUTE));
        System.out.println("秒: " + now.get(Calendar.SECOND));
        System.out.println("当前时间毫秒数:" + now.getTimeInMillis());
        System.out.println(now.getTime());
        uuId=String.valueOf(now.get(Calendar.YEAR))+String.valueOf(((now.get(Calendar.MONTH) + 1)))
        +String.valueOf(now.get(Calendar.DAY_OF_MONTH))+String.valueOf(now.get(Calendar.HOUR_OF_DAY))
        +String.valueOf(now.get(Calendar.MINUTE))+String.valueOf(now.get(Calendar.SECOND))
                +String.valueOf(getRandom(6));
        String current_time=getFomartDate("yyyyMMddHHmmss");
        uuId=current_time+String.valueOf(getRandom(6));
        System.out.println("字符串转成日期:" + uuId);
    }
复制代码

 

posted on   IT-QI  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示