Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java: Random Time generator

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.

like image 268
Martin Nemeth Avatar asked Oct 20 '25 15:10

Martin Nemeth


1 Answers

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

like image 155
Sam Avatar answered Oct 22 '25 03:10

Sam