English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article shares common methods for comparing dates, 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 used to get the current system time
Note: The author has always thought that the date type is not as flexible as the string type, so the following comparisons are all string type dates. If you are really that 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.compare two string types of dates for size
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 at dt2before return 1; else if (dt1.getTime() < dt2.getTime()) { System.out.println("dt1at dt2after"); return -1; } else { return 0; } } catch (Exception exception) { exception.printStackTrace(); } return 0; }
2.Returns the number of days between two string type 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(); } e.printStackTrace(); } long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days)); }
3.Returns the number of hours between two string type dates
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(); } e.printStackTrace(); } long between_days=(time2-time1)/(1000*3600); return Integer.parseInt(String.valueOf(between_days)); }
4.Calculate the overlapping dates of two date ranges
/** * Calculate the overlapping dates of two date ranges * @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";//Overlap startdate = str3; enddate = str2; } if (dt1.getTime()>=dt3.getTime()&&dt3.getTime()<=dt2.getTime()&&dt2.getTime()<=dt4.getTime()) { mesg = "f";//Overlap startdate = str1; enddate = str2; } if (dt3.getTime()<=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) { mesg = "f";//Overlap startdate = str1; enddate = str4; } if (dt3.getTime()>=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) { mesg = "f";//Overlap startdate = str3; enddate = str4; } System.out.println(startdate+"----"+enddate); } e.printStackTrace(); throw new ParseException(e.getMessage(), 0); } e.printStackTrace(); throw new Exception(e); } Map<String,Object> map = new HashMap<String,Object>(); map.put("startdate", startdate); map.put("enddate", enddate); return map; }
That's all for this article. I hope it will be helpful to everyone's learning and that everyone will support the呐喊 tutorial more.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @ to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)