Java生成时间随机数

文件上传或下载或许会用到,生成时间的随机数。

例:文件名+"_"+ts.getTimeRand()+扩展名。(TimeStamp ts = new TimeStamp())

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

public class TimeStamp
{
  private SimpleDateFormat sdf = null;

  public String getTimeRand()
  {
    StringBuffer buf = new StringBuffer();
    buf.append(getTimeStamp());
    Random r = new Random();
    for (int i = 0; i < 3; i++) {
      buf.append(r.nextInt(10));
    }
    return buf.toString();
  }
  public String getDate() {
    this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    return this.sdf.format(new Date());
  }
  public String getTimeStamp() {
    this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    return this.sdf.format(new Date());
  }
}

 

你可能感兴趣的:(Java,java)