• java如何随机生成定长的字符串


    小数,字符串、时间等示例代码

    String base = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
    public class RandomTest {
    
        public static Random rand = new Random();
        public static String dateStart = "2017-01-01 00:00:00";
        /**
         * hh 表示12小时制
         * HH 表示24小时制
         */
        public static SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
        public static SimpleDateFormat format1 = new SimpleDateFormat("YYYYMMDDHHmmss");
    
    
        public static void main(String[] args) throws ParseException {
            System.out.println(getDouble());
    
            long timeStart = 1483200000000l;
            System.out.println(format.parse(dateStart).getTime());
    
            System.out.println(format.format(new Date(timeStart)));
            System.out.println(format.format(new Date(timeStart+1000)));
            System.out.println(format.format(new Date(timeStart+2000)));
    
            format.format(new Date());
    
            System.out.println(getRandomString(2));
    
        }
        /**
         * 随机生成指定精确度的小数
         * @return
         */
        public static double getDouble(){
            DecimalFormat df=new DecimalFormat("#.000000");
            int a=(int)(Math.random()*2+1);
            int aa=(int)(Math.pow(-1, a));
    
            return Double.valueOf(df.format(rand.nextDouble()*100*aa));
        }
        /**
         * 生成固长字符串
         * @param length
         * @return
         */
        public static String getRandomString(int length) { //length表示生成字符串的长度
            String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";   
            Random random = new Random();   
            StringBuffer sb = new StringBuffer();   
            for (int i = 0; i < length; i++) {   
                int number = random.nextInt(base.length());   
                sb.append(base.charAt(number));   
            }   
            return sb.toString();   
         }  
        /**
         * 生成固长字符串
         * @param length
         * @return
         */
        public static String getRandomStringNum(int length) { //length表示生成字符串的长度
            String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";  
            Random random = new Random();   
            StringBuffer sb = new StringBuffer();   
            for (int i = 0; i < length; i++) {   
                int number = random.nextInt(base.length());   
                sb.append(base.charAt(number));   
            }   
            return sb.toString();   
        }
    }

    --------------------- 本文来自 AngelaPotato 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/wangxilong1991/article/details/72636482?utm_source=copy 

  • 相关阅读:
    msvc交叉编译:使用vcvarsall.bat设置命令行编译环境
    DDOS到底是什么,怎么预防,看看就明白了
    服务器安全检测和防御技术
    YApi导入swagger生成的接口
    Springboot swagger2 导出api文档
    使用Swagger2Markup归档swagger生成的API文档
    springboot + swagger2 生成api文档
    swagger2 导出离线Word/PDF/HTML文档
    Swagger使用总结
    springboot配置swagger-rest文档
  • 原文地址:https://www.cnblogs.com/YuyuanNo1/p/9705810.html
Copyright © 2020-2023  润新知