我试过使用 -
int startSeconds = restaurant.openingTime.toSecondOfDay();
int endSeconds = restaurant.closingTime.toSecondOfDay();
LocalTime timeBetweenOpenClose = LocalTime.ofSecondOfDay(ThreadLocalRandom.current().nextInt(startSeconds, endSeconds));
Run Code Online (Sandbox Code Playgroud)
但这通常会遇到错误,如 nextInt(origin, bounds), origin 不能小于 bounds,如果 myopeningTime是 16:00:00 和closingTime02:00:00 ,则会发生这种情况。