A scenario
System: xx system \ Function interface: [XXX] \ Function button: Print directly \ Order Number: XXX User feedback 2021/09/07 15:15 When printing, the system pop-up message "The modification packet has been sent, the sending time is less than 10 minutes, please operate 10 minutes later". The actual modification packet is sent by 2021-08-13 14:24, which is more than 10 minutes. Why is the message still displayedCopy the code
The second analysis
Location code
@param eirSendList * @return */ private String checkEirEmAlterSend(List<InboundEirSend> eirSendList) { StringBuilder result = new StringBuilder(); StringBuilder checkTime = new StringBuilder(" The following packet to change the box number has been sent, the sending time is less than 10 minutes, please perform the operation after 10 minutes :"); boolean isCheckTime = false; for (InboundEirSend entity : eirSendList) { int sendStatus = entity.getSendStatus(); if (sendStatus == 0) { continue; } Date sendTime = entity.getSendTime(); boolean isMore = eirSendUtilService.isMoreTimeToNow(sendTime, 10); if (isMore) { isCheckTime = true; checkTime.append(entity.getCtnNo()).append(COMMA); } } if (isCheckTime) { result.append(eirSendUtilService.subStringValue(checkTime)).append(SEMICOLON); } return result.toString(); } ` ` `Copy the code
Time determination code
@param targetDate * @param moreminute * @return */ public Boolean isMoreTimeToNow(Date targetDate, int moreMinute) { boolean result = false; if (targetDate == null) { return result; } long diffMilliseconds = System.currentTimeMillis() - targetDate.getTime(); int diffMin = (int) diffMilliseconds / 60000; if (diffMin < moreMinute) { result = true; } return result; }Copy the code
It seemed that there was no problem. I checked the product several times and found that there was indeed such a problem. Then I wrote a test code to recover the time of production and found that there was indeed a problem
int diffMin = (int) diffMilliseconds / 60000;
Copy the code
After execution, a negative number unexpectedly appears, resulting in
if (diffMin < moreMinute) {
result = true;
}
Copy the code
Result is true, which causes the problem, so why is it negative?
Int overflow problem, let’s look at int interval
– 2147483648-2147483647; Overflow problems occur if you go beyond this range, because the maximum and minimum values of a data type are cyclical, meaning that increasing the value from the maximum or decreasing the value from the minimum jumps to the opposite maximum. That explains why we have negative numbers. The last modification solved the problem by using long instead of int
public boolean isMoreTimeToNow(Date targetDate, long moreMinute) {
boolean result = false;
if (targetDate == null) {
return result;
}
long diffMilliseconds = System.currentTimeMillis() - targetDate.getTime();
long diffMin = diffMilliseconds / 60000;
if (diffMin < moreMinute) {
result = true;
}
return result;
}
Copy the code
Long ranges from -9223372036854775808 to 9223372036854775807 meet service requirements
Three conclusion
The investigation of this problem, the inspiration is that, for the problem code may be stored again, they must personally test, personally write, so as to better troubleshoot the problem.