4

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.

Martin Nemeth
  • 659
  • 3
  • 16
  • 24
  • take a help here http://stackoverflow.com/questions/11016336/how-to-generate-a-random-timestamp-in-java – Rais Alam Feb 20 '13 at 16:13

6 Answers6

6
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).

Sam
  • 1,260
  • 2
  • 11
  • 32
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.

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
2

In, java1.8

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

Amit
  • 660
  • 6
  • 8
1

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

exp2Tapavicki
  • 311
  • 3
  • 15
0

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?

il_guru
  • 8,383
  • 2
  • 42
  • 51
Khoa Nghiem
  • 315
  • 1
  • 18
0

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
Perception
  • 79,279
  • 19
  • 185
  • 195
  • I don't think there's a need for joda time here; especially since he's talking about a database and most likely the JDBC driver will expect `java.util.Date`. – Costi Ciudatu Feb 20 '13 at 16:21
  • Quite possibly so, if he is storing a TIMESTAMP in his DB then he can replace the `LocalTime` with `Date`. – Perception Feb 20 '13 at 16:23