how to generate a random timestamp in java?

2019-01-17 22:01发布

I want to generate a random timestamp and add a random increment to it to generate a second timestamp. is that possible?

If i pass random long values to create a timestamp and i want to randomly generate that long value, what would be the constraints to generate this value to give a timestamp in 2012 for example?

10条回答
三岁会撩人
2楼-- · 2019-01-17 22:06

You can generated a random timestamp by generating a random long in the appropriate range and then treating it as a millisecond-precision timestamp; e.g. new Date(long).

To determine the range, create a Date or Calendar (or similar) object that represents the start date and end date of the range, and call the long getTime() or equivalent to get the millisecond time values. Then generate a random long in that range.

查看更多
爷的心禁止访问
3楼-- · 2019-01-17 22:09

IMO, the best date time library in java is JodaTime.

If you want a random TimeStam in 2012 for example you should begin with creating the 01/01/2012 date and then add a random number of time. In the end create a TimeStamp object with the long constructor :

org.joda.time.DateTime tempDateTime = org.joda.time.DateTimeFormat.forPattern("yyyy-MM-dd").parseDateTime("2012-01-01").plusMillis(my_random_value);
return new Timestamp(tempDateTime .getMillis())
查看更多
我命由我不由天
4楼-- · 2019-01-17 22:11

Another way

public static Timestamp getRandomTime(){

  Random r = new Random();
  int Low = 100;
  int High = 1500;
  int Result = r.nextInt(High-Low) + Low;
  int ResultSec = r.nextInt(High-Low) + Low;

  Calendar calendar = Calendar.getInstance();
  calendar.add(Calendar.MINUTE, - Result);
  calendar.add(Calendar.SECOND, - ResultSec);

  java.sql.Timestamp ts = new java.sql.Timestamp(calendar.getTimeInMillis());
  return ts;
}
查看更多
干净又极端
5楼-- · 2019-01-17 22:18

Look this method:

public static Timestamp dateRandom(int initialYear, int lastYear) {
    if (initialYear > lastYear) {
        int year = lastYear;
        lastYear = initialYear;
        initialYear = year;
    }

    Calendar cInitialYear = Calendar.getInstance();
    cInitialYear.set(Calendar.YEAR, 2015);
    long offset = cInitialYear.getTimeInMillis();

    Calendar cLastYear = Calendar.getInstance();
    cLastYear.set(Calendar.YEAR, 2016);
    long end = cLastYear.getTimeInMillis();

    long diff = end - offset + 1;
    return new Timestamp(offset + (long) (Math.random() * diff));
}
查看更多
劳资没心,怎么记你
6楼-- · 2019-01-17 22:23

For your example long value to pass into Date should be between 1325397600 and 1293861599 for year 2012.Try using this site to check! To generate random date you can do something like this:

Random r =new Random();
long unixtime=(long) (1293861599+r.nextDouble()*60*60*24*365);
Date d = new Date(unixtime);
查看更多
女痞
7楼-- · 2019-01-17 22:26

Use ApacheCommonUtils to generate a random long within a given range, and then create Date out of that long.

Example:

import org.apache.commons.math.random.RandomData;
import org.apache.commons.math.random.RandomDataImpl;

public Date nextDate(Date min, Date max) {

      RandomData randomData = new RandomDataImpl();
      return new Date(randomData.nextLong(min.getTime(), max.getTime()));
}
查看更多
登录 后发表回答