Java 午夜后倒计时错误(与时区和时钟相关)

Java 午夜后倒计时错误(与时区和时钟相关),java,android,github,timer,open-source,Java,Android,Github,Timer,Open Source,我的应用程序中有几个用户报告了一个奇怪的时间重置/错误行为,我对此感到困惑,在测试和收集日志后,我发现如果我的计时器在午夜之前启动,会立即重置为20多个小时,我不知道为什么或者如何防止这种情况 该项目是开源的,欢迎任何人帮助我,以下是链接: 有关应用程序/项目的信息: 选择应用程序,选择所需的锁定时间,锁定应用程序。 与时间/日期相关的代码段 public class Timer_Service extends Service { public static final long NOTIF

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

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

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

选择应用程序,选择所需的锁定时间,锁定应用程序。 与时间/日期相关的代码段

 public class Timer_Service extends Service {

public static final long NOTIFY_INTERVAL = 1000;
public static String str_receiver = "com.nephi.getoffyourphone.receiver";
public String str_testing;
Calendar calendar;
SimpleDateFormat simpleDateFormat;
String strDate;
Date date_current, date_diff;
//Root Detector
RootBeer rootbeer;
//Con Manager
WifiManager wifiManager;
//DH Helper
DB_Helper db;
Intent intent;
Intent lockIntent;
Intent Shame;
//Shame Int Counter
private Handler mHandler = new Handler();
private Timer mTimer = null;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    //Lock Screen launch
    lockIntent = new Intent(this, locked.class);
    lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

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

    //Root Detector
    rootbeer = new RootBeer(this);
    //Wifi Manager
    wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

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

public String twoDatesBetweenTime() {


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

    }

    try {
        // Gets the first timestamp when the lockdown started
        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);

        } else {
            stopService(new Intent(getApplicationContext(), Timer_Service.class));

            mTimer.cancel();
        }
有什么建议吗

编辑:

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


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

出现此问题的原因是时间存储为H:m:ss格式,但忽略了日期方面。使用System.currentTimeMillis可以得到1970年1月1日之后的毫秒数。所以这包括日期方面。取这两个值的差值将得到已过期的毫秒数,您可以根据所用时间的任何时间间隔进行检查

改为这样做:

保存计时器启动时间时,应使用毫秒,而不是H:mm:ss:

long millisNow = System.currentTimeMillis();
将millisNow保存到数据库

并节省应花费的时间:

int minutes = 30;
long millisElapse = 30 * 60 * 1000;
将其保存到数据库,或者保存到所需的时间间隔

现在,我明白了

long diff = timeNow - timeDatabase;

if(diff > millisElapse){
    //end session
}
顺便说一句:你发布的格式是H:M:ss。大写字母M代表月份!不是几分钟。
查看有关日期和时间模式的文档

这里的代码太多,我们无法回答这个问题。请将您的问题的重点放在证明您的问题的最小值和可验证值上。当我们不知道strDate和db.get_Data1的值是什么和/或应该是什么值时,我们如何知道发生了什么?@JoeC I删除了与问题无关的代码。这更好吗?@Andreas我刚刚添加了声明的变量,够了吗?或者我应该添加更多的解释吗?基本上,我们需要的是足够的代码,没有更多的代码,我们可以复制并粘贴到我们选择的IDE中,单击Run,并在一两分钟内看到问题。哦,好吧,所以我实际上取错了两个日期的差值,我确实将我的日期保存在db中为h:m:ss。。我将尝试这个,明天调试它。非常感谢您提供的信息!即使要将整个日期保存为yyyymmddhhmms,也要使用新的SimpleDataFormat:mm:ss进行转换;仍然会返回类似于:23:55:22的内容,它没有引用日期。我已经实现了代码并测试了它-它可以在我的设备上运行,但我正在等待一些用户测试它,并将返回报告。非常感谢你的帮助!很高兴它对你有用!您的个人资料显示您位于德国。我在亚琛及其周围居住了20年。我只是受够了雨!搬回美国,真的吗?我几乎每个月都去亚琛,那里有很多人!哈哈,是的,这场雨让我感觉很糟糕>。