You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术问询:如何将预约倒计时剩余时长从时分格式改为分钟数显示

Solution: Convert Countdown to Show Total Minutes

To change your countdown from displaying a time format (like 15:30) to showing total remaining minutes (e.g., 15分钟), you'll need to calculate the time difference between the current moment and the countdown end time, then convert that difference to minutes. Here's how to adjust your code:

Modified Code Snippet

private Notification getNotification(Date countdownEnds) { 
    // Calculate the time difference in milliseconds between now and the countdown end time
    long currentMillis = System.currentTimeMillis();
    long endMillis = countdownEnds.getTime();
    long diffMillis = endMillis - currentMillis;
    
    // Convert milliseconds to total minutes, ensuring we don't get negative values
    long remainingMinutes = Math.max(0, diffMillis / (60 * 1000));
    
    // Format the remaining minutes into the desired display string
    String remainingText;
    if (remainingMinutes == 0) {
        // Handle the case where the countdown has ended or is about to start
        remainingText = resources.getString(R.string.countdown_notification_text_imminent);
    } else {
        // Use a string resource for localization (recommended)
        remainingText = String.format(resources.getString(R.string.countdown_notification_text_minutes), remainingMinutes);
        // Alternatively, if reusing the existing string resource:
        // remainingText = resources.getString(R.string.countdown_notification_text, remainingMinutes + "分钟");
    }
    
    String title = resources.getString(R.string.countdown_notification_title); 
    String text = remainingText; 
    
    // Rest of your notification building logic...
    // Pe...
}

Key Changes Explained

  • Calculate Time Difference: We subtract the current time (in milliseconds) from the countdown end time to get the remaining time in milliseconds.
  • Convert to Minutes: Divide the millisecond difference by 60 * 1000 (since there are 60 seconds in a minute, 1000 milliseconds in a second) to get total remaining minutes. Using Math.max(0, ...) ensures we don't show negative minutes if the countdown has passed.
  • Format Display String:
    • For better localization, create a new string resource like <string name="countdown_notification_text_minutes">预约开始剩余时间:%d分钟</string> in your strings.xml file.
    • Add a fallback for when the countdown ends (e.g., <string name="countdown_notification_text_imminent">预约即将开始</string>) to improve user experience.

Notes

  • If you don't need localization, you can directly use String.format("预约开始剩余时间:%d分钟", remainingMinutes) instead of string resources.
  • Make sure to handle cases where the countdown end time is in the past (hence the Math.max(0, ...) check) to avoid confusing negative values.

内容的提问来源于stack exchange,提问作者turkce apps

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:19:52