Java语言根据日期生成随机文件名

178 阅读1分钟

最近在生成文件的时候需要根据日期生成随机文件名,特此记录便于日后查阅。

package com.openailab.oascloud.file.util;

import com.openailab.oascloud.file.common.consts.BootstrapConst;

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

/**
 * @description: 文件工具
 * @author: zhangzhixiang
 * @createDate: 2020/1/7
 * @version: 1.0
 */
public class FileUtil {
    /**
     * 根据日期随机生成文件名
     *
     * @param ext
     * @return java.lang.String
     * @author zxzhang
     * @date 2019/10/8
     */
    public static String createFileName(String ext) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return simpleDateFormat.format(new Date()) + (int) (Math.random() * 900 + 100) + BootstrapConst.SPOT + (ext == null ? "" : ext);
    }
}

到此 Java语言根据日期生成随机文件名介绍完成。