java 随机时间戳_如何在Java中生成随机时间戳?

我想生成一个随机时间戳,并向其添加一个随机增量,以生成第二个时间戳。 那可能吗?

如果我传递随机的long值来创建时间戳,并且我想随机生成该long值,那么生成此值以给出2012年的时间戳的约束是什么?

您需要将随机数缩放到特定年份的范围内,并将年份的开始添加为偏移量。一年中的毫秒数从一年到另一年(le年有额外的一天,某些年份有leap分钟,依此类推),因此您可以按以下步骤确定范围:

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));

显然,此问题的最佳答案。 这似乎是一个愚蠢的问题,但是为什么在end和offset的减法中加1?

@Priidu您需要添加1以确保获得所得到的随机值的完整范围。 考虑以下示例:假设您获得了11的偏移量和15的end偏移量。 如果删除+1,则diff将为4。 因此,(long)(Math.random() * diff)将从零到3,因为Math.random()严格小于1.0。 这意味着您永远不会得到15的结果,这是不可取的。

例如,2012年要传递给Date的长值应介于1325397600和1293861599之间。请尝试使用此网站进行检查!要生成随机日期,您可以执行以下操作:

Random r =new Random();

long unixtime=(long) (1293861599+r.nextDouble()*60*60*24*365);

Date d = new Date(unixtime);

他说"例如",他不需要2012年的范围,而是需要一种通用的方式来完成任意两个日期的操作。

@Pablo:嗯,我还给了他方便的链接,以确定他想要的任何其他unix时间值。

我发现我必须将Aleksandrs答案中的纪元日期乘以1000,因为java.util.Date(long)期望纪元数以毫秒为单位,而不是秒。 所以我用了(long)(1293861599000l + r.nextDouble()* 60 * 60 * 24 * 365 * 1000);

使用ApacheCommonUtils生成给定范围内的随机长度,然后在该长度范围之外创建Date。

例:

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()));

}

首先找出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);

IMO,Java中最好的日期时间库是JodaTime。

例如,如果要在2012年使用随机的TimeStam,则应首先创建2012年1月1日,然后添加一个随机的时间。最后,使用长构造函数创建一个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())

您可以通过在适当的范围内生成随机long并将其视为毫秒精度的时间戳,例如,生成随机时间戳。 new Date(long)。

若要确定范围,请创建一个代表该范围的开始日期和结束日期的Date或Calendar(或类似的)对象,然后调用long getTime()或等效值以获取毫秒级的时间值。然后生成该范围内的随机long。

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) ;

其他方式

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;

}

看这个方法:

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));

}

以下代码生成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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值