0
  1. 所以在此之前我已经得到了两次之间的时差。
  2. 现在,我想显示将在特定时间段内收费的点,如下所示:在我的代码中,时间段命名为 diffResult,如果 diffResult 小于 30 分钟,则收费点乘以 1 diffResult =2,所以 2* 1,将收取的积分为 2。
  3. 我想使用 if else 语句,但我遇到了一些错误。这是我的代码

    pointChargeBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm a");
            Date date1 = null;
            try {
                date1 = simpleDateFormat.parse(timeResult.getText().toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date date2 = null;
            try {
                date2 = simpleDateFormat.parse(timeResult2.getText().toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String diffResult= DateUtils.getRelativeTimeSpanString(date1.getTime(), date2.getTime(), DateUtils.MINUTE_IN_MILLIS).toString();
    
            if(diffResult < 30){
    
                int point = diffResult * 2;
                pointChargeBtn.setText(point);
            }
    
    
        }
    });
    
4

1 回答 1

0

你的错误是:

 String diffResult= DateUtils.getRelativeTimeSpanString(date1.getTime(), date2.getTime(), DateUtils.MINUTE_IN_MILLIS).toString();

        if(diffResult < 30){

            int point = diffResult * 2;
            pointChargeBtn.setText(point);
        }

diffResult是 aString所以你不能将它与一个数字相比较也不能相乘

编辑

你可以这样修复它:

// difference in milliseconds
// Using Math.abs is optional. It allows us to not care about which date is the latest.
int diffInMillis = Math.abs(date2.getTime() - date1.getTime()); 

// Calculates the time in minutes
int diffInMinutes = diffInMillis / (1000 * 60);

// if difference is less (strictly) than 30 minutes
if (diffInMinutes < 30){
    // TODO: Do something
}
于 2018-05-01T13:07:12.903 回答