都是常用的日期之間的比較方法,供以後參考。
熱身:獲取當前時間
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 }