Java比较一天中自定义开始结束时间的两次

标签 java time date-format

我收到两次(格式为 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/

相关文章:

javascript - 您如何从 bigcartel 的服务器获取当前时间?

c# 日期格式不显示本地格式

java - Spring Fox Swagger UI 掩码密码

Java UTC 到本地时间不起作用

python - pandas 方式将一天中的时间(有效 datetime.time)转换为浮点变量

java - 在java中打印当前日期

java - 在 Java 中使用 DateFormat 格式化 Date 对象

mysql - 使用 FORMAT 更改日期格式

java - 谷歌云消息开发者 Android 代码崩溃

java - 错误 :(23, 17) 无法解析 : junit:junit:4. 12