天天看點

int類型資料相乘,溢出問題,負号變正号

int相乘,不做強轉的話,結果為int,溢出會出現結果不正确的情況,下面方法是個坑兒,如果要使用,需加強轉

    @Deprecated

    public static Date getDatesByDiff(Date d1, int value,int unit) {

        long s= d1.getTime();

        if(unit == UNIT_DAY) {

            return new Date(s+(1000*60*60*24*value));

        }else if(unit == UNIT_HOUR) {

            return new Date(s+(1000*60*60*value));

        }else if(unit == UNIT_MINUTE) {

            return new Date(s+(1000*60*value));

        }else {

            return new Date(s+(1000*value));

        }

    }

物聯網時代的智能生活->http://aihome.pushiwuyu.com/ 

繼續閱讀