Java 午夜后计时器重置

Java 午夜后计时器重置,java,android,time,service,calendar,Java,Android,Time,Service,Calendar,我的应用程序中有几个用户报告了一个奇怪的时间重置/错误行为,我对此感到困惑,在测试和收集日志后,我发现如果我的计时器在午夜之前启动,会立即重置为20多个小时,我不知道为什么或者如何防止这种情况 该项目是开源的,欢迎任何人帮助我,以下是链接: 有关应用程序/项目的信息: 选择应用程序,选择所需的锁定时间,锁定应用程序 与时间/日期相关的代码段 calendar = Calendar.getInstance(); simpleDateFormat = new SimpleDateFo

我的应用程序中有几个用户报告了一个奇怪的时间重置/错误行为,我对此感到困惑,在测试和收集日志后,我发现如果我的计时器在午夜之前启动,会立即重置为20多个小时,我不知道为什么或者如何防止这种情况

该项目是开源的,欢迎任何人帮助我,以下是链接:

有关应用程序/项目的信息:

  • 选择应用程序,选择所需的锁定时间,锁定应用程序

与时间/日期相关的代码段

calendar = Calendar.getInstance();
    simpleDateFormat = new SimpleDateFormat("H:M:ss");

    mTimer = new Timer();
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL);
    intent = new Intent(str_receiver);


    //DB
    db = new DB_Helper(this);
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    assert alarmService != null;
    alarmService.set(
            AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 1000,
            restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
    Log.e("Service_Auto_Restart", "ON");
}

public String twoDatesBetweenTime() {


    try {
        date_current = simpleDateFormat.parse(strDate);
    } catch (Exception e) {

    }

    try {
        date_diff = simpleDateFormat.parse(db.get_Data(1));
    } catch (Exception e) {

    }

    try {


        long diff = date_current.getTime() - date_diff.getTime();
        int int_hours = Integer.valueOf(db.get_Hours(1));
        long int_timer;
        if (int_hours > 10) {
            int_timer = TimeUnit.MINUTES.toMillis(int_hours);
        } else {
            int_timer = TimeUnit.HOURS.toMillis(int_hours);
        }
        long long_hours = int_timer - diff;
        long diffSeconds2 = long_hours / 1000 % 60;
        long diffMinutes2 = long_hours / (60 * 1000) % 60;
        long diffHours2 = long_hours / (60 * 60 * 1000) % 24;


        if (long_hours >= 0) {
            str_testing = String.format("%d:%d:%02d", diffHours2, diffMinutes2, diffSeconds2);
            Log.e("TIME", str_testing);
            db.set_TimerFinish(0);
            //db.set_Running("Y");
            fn_update(str_testing);
        } else {
            stopService(new Intent(getApplicationContext(), Timer_Service.class));
            notification_update();
            db.set_TimerFinish(1);
            db.set_Running("N");
            //db.set_LockTime("");
            db.set_Hours("");
            db.set_Data("");
            //db.set_openCounter(0);
            db.set_openTimes(0);
            mTimer.cancel();
        }
    } catch (Exception e) {
        stopService(new Intent(getApplicationContext(), Timer_Service.class));
        db.set_TimerFinish(1);
        db.set_Running("N");
        // db.set_LockTime("");
        db.set_Hours("");
        db.set_Data("");
        //db.set_openCounter(0);
        db.set_openTimes(0);
        mTimer.cancel();
        mTimer.purge();
    }

    return "";

}
有什么建议吗


编辑:

  • 计时器工作正常,如果有人设置了锁定,例如30分钟,它将倒计时30分钟,完成后解锁
假设有人在21:30:00开始锁定,计时器将正常工作,并在10:00:00结束


但是,如果有人在23:55:00开始锁定30分钟,“剩余解锁时间”将跳至21小时,而不是30分钟。只有在新的一天开始/计时器在午夜之前开始时,才会发生这种情况。

您为什么不使用
SimpleDataFormat(“HH:mm:ss”)
创建
DateFormat
?大写字母
M
表示“一年中的一个月”,我认为如果你想让时间和分钟保持零填充的话,也有必要用两个字母来表示时间和分钟(如果使用24小时时钟,你肯定会用两个字母来表示时间)。@Bobulus是的,我为特定用户更改了这一点,我发现它更好。这会更改显示的0填充。但是大写字母
M
肯定不正确吗?你肯定想要小写的
m
这意味着几分钟?@bobulus大写字母m对任何事情都没有影响,代码运行得很好,已经运行了很长时间。然而,这里唯一的问题是我在上面提到的关于几天之间/午夜之后时间重置的问题。即使你不能,你能至少为发生意外行为的情况提供准确的输入、预期结果和观察结果吗?