我正在尝试计算两个日期之间的差,但函数未返回值

我正在这样调用函数

GeneralMethods generalMethods = new GeneralMethods();
generalMethods.daysPassed(contacts_update_date, currentDate);


并且功能如下

public class GeneralMethods {

    public long daysPassed(String contacts_update_date, String currentDate) {

        // Calculate Time Passed

        long diff;
        long timePassed = 0;

        SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
        String inputString1 = contacts_update_date;
        String inputString2 = currentDate;

        try {
            Date date1 = myFormat.parse(inputString1);
            Date date2 = myFormat.parse(inputString2);
            diff = date2.getTime() - date1.getTime();
            timePassed = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return timePassed;

    }

}


最后我正在使用这样的价值

if (timePassed > 30) {
//do something
}

最佳答案

请尝试这个

GeneralMethods generalMethods = new GeneralMethods();
long timePassed=generalMethods.daysPassed(contacts_update_date, currentDate);

09-08 06:34