demand
- Find the number of days between the current day and the specified date. The specified date must be before the current day, that is, the current day is 01-23, and the specified date should be 01-22
Solution
- Use the method provided in the hutools toolkit to calculate the number of days between two dates
betweenDay()
Three parameters: between day (date, date, Boolean)
// Mode of use // cn.hutool.core.date.DateUtil.betweenDay(currentDate, targetTime, true); // Source code /** * Judge the number of days between two dates < br > * * <pre> * Sometimes we need to ignore hours, minutes and seconds when calculating the difference in days. * For example, the difference between 23:59:59 on February 1, 2016 and 00:00:00 on February 2, 2016 is one second * If isReset is < code > false < / code > the difference in days is 0. * If isReset is < code > true < / code > the number of days difference will be calculated as 1 * </pre> * * @param beginDate Start date * @param endDate End date * @param isReset Reset time to start time * @return Date difference * @since 3.0.1 */ public static long betweenDay(Date beginDate, Date endDate, boolean isReset) { if (isReset) { beginDate = beginOfDay(beginDate); endDate = beginOfDay(endDate); } return between(beginDate, endDate, DateUnit.DAY); }
Continue to the next source code, only keep the absolute value, keep the absolute value, well, it's a little pit
between
Three parameters: between(Date,Date,DateUnit)
// Note that he only retains absolute values /** * Judge the time difference between two dates, and only keep the absolute value * * @param beginDate Start date * @param endDate End date * @param unit Units of difference: days {@ link DateUnit#DAY}, hours {@ link DateUnit#HOUR}, etc * @return Date difference */ public static long between(Date beginDate, Date endDate, DateUnit unit) { return between(beginDate, endDate, unit, true); }
abs, absolute value
between()
between(Date,Date,DateUnit,boolean)
/** * Judge the time difference between two dates * * @param beginDate Start date * @param endDate End date * @param unit Units of difference: days {@ link DateUnit#DAY}, hours {@ link DateUnit#HOUR}, etc * @param isAbs Do you want to keep only positive absolute values for date intervals * @return Date difference * @since 3.3.1 */ public static long between(Date beginDate, Date endDate, DateUnit unit, boolean isAbs) { return new DateBetween(beginDate, endDate, isAbs).between(unit); }
Judge the input time, isabs & & begin After (end), after() method in
/** * Structure < br > * The date before is the start time, and the date after is the end time * * @param begin Start time * @param end End time * @param isAbs Do you want to keep only positive absolute values for date intervals * @since 3.1.1 */ public DateBetween(Date begin, Date end, boolean isAbs) { Assert.notNull(begin, "Begin date is null !"); Assert.notNull(end, "End date is null !"); if (isAbs && begin.after(end)) { // When the interval is only a positive number, if the start date is later than the end date, replace it this.begin = end; this.end = begin; } else { this.begin = begin; this.end = end; } }
Final calculation
/** * Judge the duration of the difference between two dates < br > * Returns the duration difference for a given unit * * @param unit Units of difference: days {@ link DateUnit#DAY}, hours {@ link DateUnit#HOUR}, etc * @return Time difference */ public long between(DateUnit unit) { long diff = end.getTime() - begin.getTime(); return diff / unit.getMillis(); }
The source code of after called above
abs source code
public static int abs(int a) { return (a < 0) ? -a : a; }
There is a small problem bug
- If the above processing method is used, the calculated number of days between two dates is always positive
- for example
- Today is 2022-01-23, the target date is 2022-01-22, and the number of days difference is positive 1
- Today is 2022-01-23, the target date is 2022-01-24, and the number of days difference is also positive 1
- Then it will appear that the value obtained by the timeout day and the adjacent day is the same, which is inconsistent with the demand
Repair scheme
Directly call the between(Date,Date,DateUnit,boolean) method in the above source code. Do you need abs to pass it in
int betweenDay = (int) cn.hutool.core.date.DateUtil.between(currentDate, targetTime, DateUnit.DAY, false);
Interpretation: from the start time to the end time, the difference is calculated according to the number of days. isAbs is false, and the absolute value operation is not performed
Then the values of 01-20 and today's 01-24 are - 4 days
Then the values of 01-26 and today's 01-24 are 1 day
There's a problem again. I didn't convert time to days. How can 26 - 24 be equal to 1
Modify again
Date beginDate = cn.hutool.core.date.DateUtil.beginOfDay(currentDate); Date endDate = cn.hutool.core.date.DateUtil.beginOfDay(targetTime); System.out.println(beginDate); System.out.println(endDate); int betweenDay = (int) cn.hutool.core.date.DateUtil.between(beginDate, endDate, DateUnit.DAY, false);
Finally, the world is clean, negative numbers come out, and positive numbers are normal
Complete test code:
public static void main(String[] args) { String dateStr = "2022-01-21 00:00:00"; String dateStr2 = "2022-01-26 00:00:00"; String currentDateString = "2022-01-24 11:01:00"; //Date currentDate = DateUtil.getCurrentDate(); System.out.println(dateStr); DateTime targetTime = cn.hutool.core.date.DateUtil.parse(dateStr, "yyyy-MM-dd HH:mm:ss"); DateTime currentDate = cn.hutool.core.date.DateUtil.parse(currentDateString, "yyyy-MM-dd HH:mm:ss"); System.out.println(targetTime); System.out.println(currentDate); Date beginDate = cn.hutool.core.date.DateUtil.beginOfDay(currentDate); Date endDate = cn.hutool.core.date.DateUtil.beginOfDay(targetTime); System.out.println(beginDate); System.out.println(endDate); int betweenDay = (int) cn.hutool.core.date.DateUtil.between(beginDate, endDate, DateUnit.DAY, false); System.out.println(betweenDay); }
It also needs to carry out space judgment and exception handling according to the actual needs
Extension, for date After and date Test of befer
public static void main(String[] args) { String dateStr = "2022-01-21 00:00:00"; String dateStr2 = "2022-01-26 00:00:00"; String currentDateString = "2022-01-24 11:01:00"; //Date currentDate = DateUtil.getCurrentDate(); System.out.println(dateStr); DateTime targetTime = cn.hutool.core.date.DateUtil.parse(dateStr, "yyyy-MM-dd HH:mm:ss"); DateTime currentDate = cn.hutool.core.date.DateUtil.parse(currentDateString, "yyyy-MM-dd HH:mm:ss"); System.out.println("targetTime..."+targetTime); System.out.println("currentDate..."+currentDate); boolean after = targetTime.after(currentDate); System.out.println("targetTime.after(currentDate)..."+after); boolean before = targetTime.before(currentDate); System.out.println("targetTime.before(currentDate)..."+before); }
Output result:
2022-01-21 00:00:00 targetTime...2022-01-21 00:00:00 currentDate...2022-01-24 11:01:00 targetTime.after(currentDate)...false targetTime.before(currentDate)...true