你的位置:首页 > Java教程

[Java教程]java日期比较,日期计算


都是常用的日期之间的比较方法,供以后参考。

热身:获取当前时间

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String nowDate = df.format(new Date());// new Date()为获取当前系统时间

注:楼主一直以为date类型不如string类型随心所欲,所以下面的比较都是string类型的date进行比较。如果你真的真的就是那么倔强,ok,下面是date转string的方法:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String date = df.format(Date类型的时间);

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

 1 public static int compare_date(String DATE1, String DATE2) { 2     DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 3     try { 4       Date dt1 = df.parse(DATE1); 5       Date dt2 = df.parse(DATE2); 6       if (dt1.getTime() > dt2.getTime()) { 7         System.out.println("dt1 在dt2前"); 8         return 1; 9       } else if (dt1.getTime() < dt2.getTime()) {10         System.out.println("dt1在dt2后");11         return -1;12       } else {13         return 0;14        }15     } catch (Exception exception) {16        exception.printStackTrace();17      }18     return 0;19   }

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

 1 public static int daysBetween(String smdate,String bdate){  2     SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  3     Calendar cal = Calendar.getInstance();   4     long time1 = 0; 5     long time2 = 0; 6      7     try{ 8        cal.setTime(sdf.parse(smdate));   9        time1 = cal.getTimeInMillis();  10        cal.setTime(sdf.parse(bdate)); 11        time2 = cal.getTimeInMillis(); 12     }catch(Exception e){13        e.printStackTrace();14      }15     long between_days=(time2-time1)/(1000*3600*24); 16       17     return Integer.parseInt(String.valueOf(between_days));   18   } 

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

 1 public static int daysBetween2(String startTime, String endTime) { 2      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");  3      Calendar cal = Calendar.getInstance();   4      long time1 = 0; 5      long time2 = 0; 6       7      try{ 8         cal.setTime(sdf.parse(startTime));   9        time1 = cal.getTimeInMillis();  10         cal.setTime(sdf.parse(endTime)); 11        time2 = cal.getTimeInMillis(); 12      }catch(Exception e){13        e.printStackTrace();14      }15      long between_days=(time2-time1)/(1000*3600); 16        17     return Integer.parseInt(String.valueOf(between_days));   18    }

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

 1 /** 2    * 计算两段日期的重合日期 3    * @param str1 开始日期1 4    * @param str2 结束日期1 5    * @param str3 开始日期2 6    * @param str4 结束日期2 7    * @return 8    * @throws Exception 9   */10   public static Map<String,Object> comparisonRQ(String str1, String str2, String str3,11       String str4) throws Exception {12     String mesg = "";13     DateFormat df = new SimpleDateFormat("yyyy-MM-dd");14     String startdate = "";15     String enddate = "";16     try {17       Date dt1 = df.parse(str1);18       Date dt2 = df.parse(str2);19       Date dt3 = df.parse(str3);20       Date dt4 = df.parse(str4);21       if (dt1.getTime()<=dt3.getTime()&&dt3.getTime()<=dt2.getTime()&&dt2.getTime()<=dt4.getTime()) {22         mesg = "f";//重合23         startdate = str3;24         enddate = str2;25       }26       if (dt1.getTime()>=dt3.getTime()&&dt3.getTime()<=dt2.getTime()&&dt2.getTime()<=dt4.getTime()) {27         mesg = "f";//重合28         startdate = str1;29         enddate = str2;30       }31       32       if (dt3.getTime()<=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) {33         mesg = "f";//重合34         startdate = str1;35         enddate = str4;36       }37       if (dt3.getTime()>=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) {38         mesg = "f";//重合39         startdate = str3;40         enddate = str4;41       }42       43       System.out.println(startdate+"----"+enddate);44       45       46     }catch (ParseException e) {47       e.printStackTrace();48       throw new ParseException(e.getMessage(), 0);49     }catch(Exception e){50       e.printStackTrace();51       throw new Exception(e);52     }53     Map<String,Object> map = new HashMap<String,Object>();54     map.put("startdate", startdate);55     map.put("enddate", enddate);56     return map;57   }