FileCreateNameUtils.java 1.22 KB
Newer Older
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
package cn.wisenergy.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by m1991 on 2021/2/26 11:10
 */
public class FileCreateNameUtils {
    public static final String numberChar = "0123456789";

    /***
     * 文件名生成工具类
     */

    public static String toCreateName() {

        return getNowDatetoString() + generateNum(10);

    }

    /***
     * 生成日期字符串 yyyyMMddHHmm
     *
     * @author MRC
     * @date 2019年4月16日下午2:19:37
     * @return
     */
    public static String getNowDatetoString() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /***
     * 生成随机数
     * @author MRC
     * @date 2019年4月16日下午2:21:06
     * @param len
     * @return
     */
    public static String generateNum(int len) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < len; i++) {
            sb.append(numberChar.charAt(random.nextInt(numberChar.length())));
        }
        return sb.toString();
    }

}