【发布时间】:2023-03-24 16:47:01
【问题描述】:
我尝试过使用 -
int startSeconds = restaurant.openingTime.toSecondOfDay();
int endSeconds = restaurant.closingTime.toSecondOfDay();
LocalTime timeBetweenOpenClose = LocalTime.ofSecondOfDay(ThreadLocalRandom.current().nextInt(startSeconds, endSeconds));
但这通常会在 nextInt(origin, bounds) 中遇到错误,如果我的 openingTime 是 16:00:00 并且 closingTime 是 02:00,则原点不能小于 bounds: 00.
【问题讨论】:
-
我是否正确假设
restaurant.openingTime和restaurant.closingTime也是java.time.LocalTime? -
是的,没错。此外,下午 4 点属于今天,凌晨 2 点属于午夜后,即第二天。 @OleV.V.
标签: java random time java-time localtime