我收到两次(格式为 HH:mm aa),即下午 6:00 到下午 5:00。我需要一些通用且最短的方法来比较这两个时间。
例如:-
假设我正在比较下午 4 点和下午 7 点,那么我应该得到下午 4 点 > 晚上 7 点,因为我的开始时间是下午 6 点,结束时间是下午 5 点
请帮忙。
最佳答案
// Per the JavaDoc:
// the value 0 if the argument Date is equal to this Date; a value
// less than 0 if this
// Date is before the Date argument; and a value greater
// than 0 if this Date is after
// the Date argument.
if (startDate.compareTo(endDate) < 0)
{
// before
}
else if (startDate.compareTo(endDate) == 0)
{
// same
}
else if (startDate.compareTo(endDate) > 0)
{
// after
}
else if (startDate.compareTo(firstDate) > 0 && startDate.compareTo(secondDate) < 0)
{
// between
}
根据需要插入您的条件。
关于Java比较一天中自定义开始结束时间的两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22172377/