Java判断时间是否为半夜(23:59与00:01之间)及常用时间比较方法

Java中判断时间是否半夜的方法,具体指的是半夜12点正负一分钟,也就是23:59与00:01之间。以及时间比较的方法 。本文主要介绍判断时间是否在特定范围内以及一些常用的时间比较方法。

1、错误示例

private final LocalTime ONE_MINUTE_BEFORE_MIDNIGHT = LocalTime.of(23, 59, 0);
private final LocalTime ONE_MINUTE_AFTER_MIDNIGHT = LocalTime.of(0, 1, 0);
public boolean isAtMidnight(LocalTime time) { return time.isAfter(ONE_MINUTE_BEFORE_MIDNIGHT) && time.isBefore(ONE_MINUTE_AFTER_MIDNIGHT); }

上面代码有问题,总是返回False,这里要特别注意不能使用&&符号。

2、正确代码

public boolean isAtMidnight(LocalTime time){
return time.isAfter(ONE_MINUTE_BEFORE_MIDNIGHT) || time.isBefore(ONE_MINUTE_AFTER_MIDNIGHT);
}
public static void main(String[] args) { LocalTime ONE_MINUTE_BEFORE_MIDNIGHT = LocalTime.of(23, 59, 0); LocalTime ONE_MINUTE_AFTER_MIDNIGHT = LocalTime.of(0, 1, 0); LocalTime md = LocalTime.MIDNIGHT; System.out.println(md.isBefore(ONE_MINUTE_AFTER_MIDNIGHT) || md.isAfter(ONE_MINUTE_BEFORE_MIDNIGHT)); }

3、时间比较方法

1)两个string类型的日期比较大小

public static int compare_date(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() > dt2.getTime()) {
System.out.println("dt1 在dt2前");
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
System.out.println("dt1在dt2后");
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}

2)返回两个string类型日期之间相差的天数

public static int daysBetween(String smdate,String bdate){  
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
long time1 = 0;
long time2 = 0;
try{
cal.setTime(sdf.parse(smdate));
time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
time2 = cal.getTimeInMillis();
}catch(Exception e){
e.printStackTrace();
}
long between_days=(time2-time1)/(1000*3600*24);
return Integer.parseInt(String.valueOf(between_days));
}

3)返回两个string类型日期相差的小时数

public static int daysBetween2(String startTime, String endTime) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");
Calendar cal = Calendar.getInstance();
long time1 = 0;
long time2 = 0;
try{
cal.setTime(sdf.parse(startTime));
time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(endTime));
time2 = cal.getTimeInMillis();
}catch(Exception e){
e.printStackTrace();
}
long between_days=(time2-time1)/(1000*3600);
return Integer.parseInt(String.valueOf(between_days));
}

4)计算两段日期的重合日期

/**
     * 计算两段日期的重合日期
     * @param str1 开始日期1
     * @param str2 结束日期1
     * @param str3 开始日期2
     * @param str4 结束日期2
     * @return
     * @throws Exception
     */
    public static Map<String,Object> comparisonRQ(String str1, String str2, String str3,
            String str4) throws Exception {
        String mesg = "";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String startdate = "";
        String enddate = "";
        try {
            Date dt1 = df.parse(str1);
            Date dt2 = df.parse(str2);
            Date dt3 = df.parse(str3);
            Date dt4 = df.parse(str4);
            if (dt1.getTime()<=dt3.getTime()&&dt3.getTime()<=dt2.getTime()&&dt2.getTime()<=dt4.getTime()) {
                mesg = "f";//重合
                startdate = str3;
                enddate = str2;
            }
            if (dt1.getTime()>=dt3.getTime()&&dt3.getTime()<=dt2.getTime()&&dt2.getTime()<=dt4.getTime()) {
                mesg = "f";//重合
                startdate = str1;
                enddate = str2;
            }
            if (dt3.getTime()<=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) {
                mesg = "f";//重合
                startdate = str1;
                enddate = str4;
            }
            if (dt3.getTime()>=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) {
                mesg = "f";//重合
                startdate = str3;
                enddate = str4;
            }
            System.out.println(startdate+"----"+enddate);
        }catch (ParseException e) {
            e.printStackTrace();
            throw new ParseException(e.getMessage(), 0);
        }catch(Exception e){
            e.printStackTrace();
            throw new Exception(e);
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("startdate", startdate);
        map.put("enddate", enddate);
        return map;
    }

推荐阅读
cjavapy编程之路首页