Java Utililty Methods Random Date

List of utility methods to do Random Date

Description

The list of methods to do Random Date are organized into topic(s).

Method

StringgenerateRandomDate()
generate Random Date
Random r = new Random();
java.util.Calendar c = java.util.Calendar.getInstance();
c.set(java.util.Calendar.MONTH, Math.abs(r.nextInt()) % 12);
c.set(java.util.Calendar.DAY_OF_MONTH, Math.abs(r.nextInt()) % 30);
c.setLenient(true);
return DATE_FORMAT.format(c.getTime());
StringgetDateRandom()
get Date Random
String s = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
return s;
StringgetDateRand(Date date)
get Date Rand
String str = sdf.format(date);
double d = Math.random();
int t = 1000 + (int) (d * 9000.0D);
return str + t;
StringgetDateRand()
get Date Rand
Calendar c = Calendar.getInstance(Locale.CHINA);
Date d = c.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(d) + (int) (Math.random() * 10);
StringgetDateRandomId()
return a ID contruct on with the current date : sample : 20101223-4687
DateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.US);
NumberFormat formatter = new DecimalFormat("00000");
String newDateId;
do {
    Calendar today = Calendar.getInstance();
    int daySec = today.get(Calendar.HOUR_OF_DAY) * 24 * 60 + today.get(Calendar.MINUTE) * 60
            + today.get(Calendar.SECOND);
    String currentDate = format.format(today.getTime());
...
StringgetRandomDate(Random ran, boolean idNewID)
get Random Date
if (ran == null) {
    return "";
int year = 0;
if (idNewID) {
    year = 1900 + ran.nextInt(2007 - 1900);
} else {
    year = 1 + ran.nextInt(99);
...
StringgetRandomDate()
get Random Date
long time = System.currentTimeMillis();
Date dat = new Date(time);
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(dat);
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return format.format(gc.getTime());