我想生成一个随机时间戳并向其添加一个随机增量以生成第二个时间戳。那可能吗?
如果我传递随机长值来创建时间戳并且我想随机生成该长值,那么生成该值以在 2012 年给出时间戳的约束是什么?
您需要将随机数缩放到特定年份的范围内,并将年份的开始添加为偏移量。一年中的毫秒数从一年到另一年变化(闰年有额外的一天,某些年份有闰分钟,等等),因此您可以在缩放之前确定范围,如下所示:
long offset = Timestamp.valueOf("2012-01-01 00:00:00").getTime();
long end = Timestamp.valueOf("2013-01-01 00:00:00").getTime();
long diff = end - offset + 1;
Timestamp rand = new Timestamp(offset + (long)(Math.random() * diff));
对于您的示例,传递给 Date 的长值应介于2012 年的1325397600和1293861599之间。尝试使用此站点进行检查!要生成随机日期,您可以执行以下操作:
Random r =new Random();
long unixtime=(long) (1293861599+r.nextDouble()*60*60*24*365);
Date d = new Date(unixtime);
使用 ApacheCommonUtils 在给定范围内生成随机长整数,然后在该长整数中创建日期。
例子:
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()));
}
long
您可以通过在适当范围内生成随机数,然后将其视为毫秒精度的时间戳来生成随机时间戳;例如new Date(long)
。
要确定范围,请创建表示范围的开始日期和结束日期的日期或日历(或类似)对象,并调用long getTime()
或等效项以获取毫秒时间值。long
然后在该范围内生成一个随机数。
IMO,Java 中最好的日期时间库是 JodaTime。
例如,如果您想要一个 2012 年的随机 TimeStam,您应该从创建 01/01/2012 日期开始,然后添加一个随机数的时间。最后使用 long 构造函数创建一个 TimeStamp 对象:
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())
首先找出 2012 年的开始:
long start2012 = new SimpleDateFormat("yyyy").parse("2012").getTime();
获取一年中的随机毫秒数:
final long millisInYear2012 = 1000 * 60 * 60 * 24 * 365 + 1000; // Have to account for the leap second!
long millis = Math.round(millisInYear2012 * Math.random());
Timestamp timeStamp = new Timestamp(start2012 + millis);
以下代码生成 2012 年的随机时间戳。
DateFormat dateFormat = new SimpleDateFormat("yyyy");
Date dateFrom = dateFormat.parse("2012");
long timestampFrom = dateFrom.getTime();
Date dateTo = dateFormat.parse("2013");
long timestampTo = dateTo.getTime();
Random random = new Random();
long timeRange = timestampTo - timestampFrom;
long randomTimestamp = timestampFrom + (long) (random.nextDouble() * timeRange);
看看这个方法:
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));
}
其他方式
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;
}
import org.joda.time.*;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
DateTime d1 =DateTime.now().withZone(DateTimeZone.UTC);
DateTime d0 = d1.minusSeconds(20);
DateTime d2 = d1.plusSeconds(20);
Random r = new Random();
long t1 = d0.getMillis();
long t2 = d2.getMillis();
//DateTime d1 = new DateTime(t1);
//DateTime d2 = new DateTime(t2);
Random random = new Random();
long rand = t1 + (long) (random.nextDouble() * (t2-t1));
DateTime randDatetime = new DateTime(rand);
String datestr= randDatetime.toString("MM/dd/YYYY hh:mm:ss") ;
System.out.println(datestr) ;
我实际上是在 scala 中编码,但无论如何它可能很有用
import scala.util.Random
import java.sql.Timestamp
def generateRandomTimestamp(dateStringMin: String = "2000-01-01 00:00:00", dateStringMax: String = "2021-12-31 00:00:00"): java.sql.Timestamp = {
val date1 = Timestamp.valueOf(dateStringMin).getTime()
val date2 = Timestamp.valueOf(dateStringMax).getTime()
val diff = date2 - date1
new Timestamp(date1 + (Random.nextFloat() * diff).toLong)}