Don't copy code on the Internet! A piece of code looking for on the Internet suddenly exploded!

Keywords: Programming Java Google REST network

Meet a demand, to serve some of the requirements of the players to send props reward, reward the number of days according to offline calculation.

This requirement is very simple to realize. It only needs to calculate the last offline time and the current time interval when the player is online, then calculate the type and quantity of props according to the planned algorithm, and send an email to the player.

There is no ready-made function for calculating the number of days between two time intervals, and I am too lazy to write it. So I went to Google and searched the first result. The code is as follows

public static int differentDays(Date date1,Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
       int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2)   //Same year
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0)    //Leap year
                {
                    timeDistance += 366;
                }
                else    //Not a leap year.
                {
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2-day1) ;
        }
        else    //Different years
        {
            System.out.println("judge day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }

Code source: https://www.cnblogs.com/0201zcr/p/5000977.html

Copy the code into the project, debug it, and use it directly if there is no problem. After all, Google is the first one in the results, rest assured.

This code has been running for several months without any problem, but on the day of January 1, 2020, some players reported that they received hundreds of reward emails. They were very happy, but out of their love for the game, they informed the operators.

After the operation fed back the bug to the server, I began to check it. What I couldn't think of was that the server hasn't been updated in recent days, but the server was stable in the previous days. How could the bug suddenly appear.

The next step is to analyze the player's data, determine the problem with the code logic, and finally eliminate all possibilities according to the performance of BUG, and find that the only possible problem is the function copied from the Internet to calculate the difference in days.

According to the debugging results, this function will return a wrong result when two date parameters are different years and the first date is greater than the second date

such as

Different days ("2020-1-1", "2019-12-25") theoretically, the correct result is - 7, but because of the bug in the function, the result is 358

Therefore, there was no need to issue rewards, because this special situation sent out 358 copies at once, which seriously affected the balance of certain props in the game.

As for the remedy method, it is to count the list, recycle the props that have been sent out but not used, treat the used items as welfare, then issue an apology, and send some other items to make up.

Fortunately, the remedy is timely. If these props can't be recovered, the strategy of game operation will be greatly changed. I'm sure there's no good fruit to eat.

So don't copy the unknown code on the Internet. If you really want to use it, you must test it again and again. Otherwise, you will suffer from the sudden thunder one day.

BUG fixed using java 8's date library instead

public static int differentDays(Date date1, Date date2) {
    if (date1 == null || date2 == null) {
        throw new RuntimeException("Date cannot be empty");
    }
    LocalDate localDate1 = date2LocalDate(date1);
    LocalDate localDate2 = date2LocalDate(date2);
    return Generic.long2int(localDate1.until(localDate2, ChronoUnit.DAYS));
}
​
public static LocalDate date2LocalDate(Date date) {
    Instant instant = date.toInstant();
    ZoneId zoneId = ZoneId.systemDefault();
    LocalDate localDate = instant.atZone(zoneId).toLocalDate();
    return localDate;
}

Original link: https://www.cnblogs.com/aspwebchh/p/12220673.html Source network, only for learning, if there is infringement, contact delete.

I have collected high-quality technical articles and experience summary in my official account [Java circles].

To facilitate your study, I also arranged a set of learning materials, including Java virtual machine, spring framework, java thread, data structure, design mode, etc., which are provided free of charge to students who love Java~

file

Posted by calexa on Wed, 15 Apr 2020 00:15:32 -0700