java: Random Time generator

2019-07-24 06:06发布

问题:

How do I generate random Time values? For example: 07:02:33, 08:52:14, etc. I know how to generate random numbers but I don't know how to do this. I want to fill my database column TIME with random values.

回答1:

import java.util.Random;
import java.sql.Time;

final Random random = new Random();
final int millisInDay = 24*60*60*1000;
Time time = new Time((long)random.nextInt(millisInDay));

For your purposes this might be enough. Don't forget that some days have different lengths for which you might need to add test cases (daylight savings and leap seconds).



回答2:

A java.util.Date is merely a wrapper around a long value (milliseconds since the epoch). Therefore, you could simply generate random long values with Random.nextLong(), and wrap the result within a new Date(result). These date instances you can pass to your JDBC driver.



回答3:

In, java1.8

Random generator = new Random(seed); LocalTime time = LocalTime.MIN.plusSeconds(generator.nextLong());



回答4:

Using Random Generator like one here RandomUtil class you can make random dates between some values and much more.

Code examples using this class:

If you need to update time from existing date you can use code like this. Just replace System.currentTimeMillis() with date from database.

java.util.Date dateFromDB = new java.util.Date(System.currentTimeMillis());

Calendar calendarFromDB = Calendar.getInstance();

calendarFromDB.setTime(dateFromDB);

java.util.Date randomDate = RandomUtil.getRandomDate(new java.util.Date(RandomUtil.getMinimumDate()), new java.util.Date(RandomUtil.getMaximumDate()), false);
Calendar calendar=Calendar.getInstance();
calendar.setTime(randomDate);

calendarFromDB.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
calendarFromDB.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE));
calendarFromDB.set(Calendar.SECOND, calendar.get(Calendar.SECOND));

dateFromDB = calendarFromDB.getTime();

Sample output:

Tue Jul 26 02:30:27 CET 157737154

or if you want just random date or time between some dates

java.util.Date randomDate = RandomUtil.getRandomDate(new java.util.Date(RandomUtil.getMinimumDate()), new java.util.Date(RandomUtil.getMaximumDate()), false);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println(sdf.format(randomDate));

Sample output:

22:29:15



回答5:

If you can use a third party library heres a way to do it using Joda Time. You will need to tweak the code to fit your scenario:

final Random random = new Random();
for (int i = 0; i < 10; i++) {
    final LocalTime time = new LocalTime(random.nextLong());
    System.out.println(time);
}

Sample output:

01:58:24.328
10:59:20.576
07:52:40.011
11:53:54.524
13:43:57.474
21:51:25.032
11:46:35.988
16:20:20.224
09:47:10.404
22:35:43.337


回答6:

Without looking at how to generate random timestamps, if you know how to generate random numbers, how about you generate 3 random numbers each time so that you can make a random time value?