【发布时间】:2018-04-10 10:41:36
【问题描述】:
我有一个关于在线餐厅订单的android 项目。我在时间逻辑上遇到了一些问题
我通过String 得知餐厅的营业或关闭时间:
- 餐厅 1:
open at "16:00"和close at "02.00" - 餐厅 2:
open at "02.00"和close at "16:00"
如果这次at "18:00" 餐厅 1 应该开门了。
我已经像这样尝试过下面的代码, 但是餐厅 1 仍然关闭:
val open = "16:00"
val close = "02:00"
val calendar = Calendar.getInstance()
val time = calendar.time
val currentTime: String = SimpleDateFormat("HH:mm").format(time)
if(currentTime.compareTo(open) >= 0 currentTime.compareTo(close) < 0){
// do something is open
}
else{
// do something is close
}
我使用kotlin,也许有人可以帮助我也使用java
【问题讨论】:
-
你确定 "currentTime.compareTo(open) >= 0 currentTime.compareTo(close)
-
还是
||?取决于关闭时间是否超过午夜。还有@ValentinMichalak -
我建议你避免使用
SimpleDateFormat类。它不仅过时了,而且出了名的麻烦。今天我们在java.time, the modern Java date and time API 的表现要好得多。
标签: java android time kotlin compare