This article shares common methods of comparing dates between commonly used for future reference.
Warm up: Get the current time
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//Set date format
String nowDate = df.format(new Date());// new Date() is to get the current system time
Note: The author always thought that the date type is not as good as the string type, so the following comparisons are all about the date of the string type. If you are really so stubborn, OK, here is the method to convert date to string:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//Set date format
String date = df.format(Date type time);
1. Comparison of the size of the dates of two string types
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 before dt2"); return 1; } else if (dt1.getTime() < dt2.getTime()) { System.out.println("dt1 is after dt2"); return -1; } else { return 0; } } catch (Exception exception) { exception.printStackTrace(); } return 0; } 2. Return the number of days that differ between two string types dates
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. Return the number of hours that differ between two string types
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. Calculate the overlapping dates of two dates
/** * Calculate the date of the overlap between two dates* @param str1 Start date1 * @param str2 End date1 * @param str3 Start date2 * @param str4 End date2 * @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";//Coincid startdate = str3; enddate = str2; } if (dt1.getTime()>=dt3.getTime()&&dt3.getTime()<=dt2.getTime()&&dt2.getTime()<=dt4.getTime()) { mesg = "f";//coincid startdate = str1; enddate = str2; } if (dt3.getTime()<=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) { mesg = "f";//coincid startdate = str1; enddate = str4; } if (dt3.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) { mesg = "f";//coincid startdate = str1; enddate = str4; } if (dt3.getTime()>=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) { mesg = "f";//Coincidence 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; }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.