我在一些项目中工作,如果在晚上 8 点到凌晨 12 点之间的时间段之间,需要测试特定的时间。我将日期转换为long
并检查差异。对于所有其他时间段,我得到了积极的输出。但是当我在晚上 8 点到凌晨 12 点进行测试时,我没有得到任何输出。下面是我的代码:
package com.google.pulldata.utils;
import java.text.SimpleDateFormat;
public class Test2 {
public static void main(String[] args) throws Exception{
try {
process1();
process2();
}catch(Exception ex) {
ex.printStackTrace();
}
}
private static void process1() throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_hh_a");
String dateRange1="2018_11_18_04_PM";
String dateRange2="2018_11_18_08_PM";
long dtRange1=sdf.parse(dateRange1).getTime();
long dtRange2=sdf.parse(dateRange2).getTime();
String dateTest = "2018_11_18_06_PM";
long dtTest=sdf.parse(dateTest).getTime();
if(dtTest>=dtRange1 && dtTest<=dtRange2) {
System.out.println("yes time is between 04 PM to 08 PM slot");
}
}
private static void process2() throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_hh_a");
String dateRange1="2018_11_18_08_PM";
String dateRange2="2018_11_18_12_AM";
long dtRange1=sdf.parse(dateRange1).getTime();
long dtRange2=sdf.parse(dateRange2).getTime();
String dateTest = "2018_11_18_10_PM";
long dtTest=sdf.parse(dateTest).getTime();
if(dtTest>=dtRange1 && dtTest<=dtRange2) {
System.out.println("yes time is between 08 PM to 12 AM slot");
}
}
}
输出:
yes time is between 04 PM to 08 PM slot
如您所见,我得到的是process1()
方法的输出,而不是“proccess2()”的输出。谁能帮我解决为什么会这样?