samedi 3 avril 2021

How to generate a random time between two times say 4PM and 2AM?

I have tried using -

int startSeconds = restaurant.openingTime.toSecondOfDay();
int endSeconds = restaurant.closingTime.toSecondOfDay();
LocalTime timeBetweenOpenClose = LocalTime.ofSecondOfDay(ThreadLocalRandom.current().nextInt(startSeconds, endSeconds));

But this usually runs into an error as in nextInt(origin, bounds), origin can't be less than bounds which will happen if my openingTime is 16:00:00 and closingTime is 02:00:00.

Kindly help me with this.. Thanks!!




Aucun commentaire:

Enregistrer un commentaire