JAVA随机生成文件名:当前年月日时分秒+五位随机数

来源:互联网 发布:mac升级失败系统丢失 编辑:程序博客网 时间:2024/04/30 00:54


代码如下:


package cn.gov.csrc.util;

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

public class RandomUtil {

 /**
  * 生成随机文件名:当前年月日时分秒+五位随机数
  *
  * @return
  */
 public static String getRandomFileName() {

  SimpleDateFormat simpleDateFormat;

  simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

  Date date = new Date();

  String str = simpleDateFormat.format(date);

  Random random = new Random();

  int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数

  return rannum + str;// 当前时间
 }

 public static void main(String[] args) {

  String fileName = RandomUtil.getRandomFileName();

  System.out.println(fileName);//8835920140307
 }
}


0 0
原创粉丝点击