Countdown timer bug after midnight (Related to Timezone and Clock )
I've got a couple of users in my app reporting a weird behavior of a time reset/bug, i was baffled by it and after testing and collecting logs - i figured that my timer, if started before midnight, resets to 20+ hours instantly, i've can't figure out why or how to prevent this.
The project is open source and anyone is welcome to help me with this, here are the links:
GitHub
Info about the app/project:
- Select apps, select desired lock-down time, lock apps.
My Timer Class ( Full )
Time/Date related snippet
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();
}
Any suggestions ?
EDIT:
- Timer works fine, if someone sets the lockdown for example, 30 minutes, it will countdown 30 minutes and unlock when it's done.
Let's say someone started the lockdown at 21:30:00, timer will work just fine and end at 10:00:00.
However, if someone started the lockdown at 23:55:00 for 30 minutes, the "Time left to unlock" will jump to 21 hours, instead of 30 minutes. This only happens when a new day starts/timer starts before midnight.
java

|
show 9 more comments
I've got a couple of users in my app reporting a weird behavior of a time reset/bug, i was baffled by it and after testing and collecting logs - i figured that my timer, if started before midnight, resets to 20+ hours instantly, i've can't figure out why or how to prevent this.
The project is open source and anyone is welcome to help me with this, here are the links:
GitHub
Info about the app/project:
- Select apps, select desired lock-down time, lock apps.
My Timer Class ( Full )
Time/Date related snippet
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();
}
Any suggestions ?
EDIT:
- Timer works fine, if someone sets the lockdown for example, 30 minutes, it will countdown 30 minutes and unlock when it's done.
Let's say someone started the lockdown at 21:30:00, timer will work just fine and end at 10:00:00.
However, if someone started the lockdown at 23:55:00 for 30 minutes, the "Time left to unlock" will jump to 21 hours, instead of 30 minutes. This only happens when a new day starts/timer starts before midnight.
java

There is far too much code here for us to be able to answer this question. Please edit your question to include a Minimal, Complete, and Verifiable example (focus on "Minimal" and "Verifiable") that demonstrates your issue.
– Joe C
Jan 1 at 21:39
How are we supposed to know what's going on, when we don't know what values ofstrDate
anddb.get_Data(1)
are and/or are supposed to be?
– Andreas
Jan 1 at 21:41
@JoeC I deleted codes that are not related to the issue. Is that better?
– XerXes
Jan 1 at 21:46
@Andreas I just added the declared variables, is it enough? Or should i add more explanation ?
– XerXes
Jan 1 at 21:47
2
Basically, what we need is enough code (and no more) that we can copy and paste into our IDE of choice, click Run, and see the issue within a minute or two.
– Joe C
Jan 1 at 21:52
|
show 9 more comments
I've got a couple of users in my app reporting a weird behavior of a time reset/bug, i was baffled by it and after testing and collecting logs - i figured that my timer, if started before midnight, resets to 20+ hours instantly, i've can't figure out why or how to prevent this.
The project is open source and anyone is welcome to help me with this, here are the links:
GitHub
Info about the app/project:
- Select apps, select desired lock-down time, lock apps.
My Timer Class ( Full )
Time/Date related snippet
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();
}
Any suggestions ?
EDIT:
- Timer works fine, if someone sets the lockdown for example, 30 minutes, it will countdown 30 minutes and unlock when it's done.
Let's say someone started the lockdown at 21:30:00, timer will work just fine and end at 10:00:00.
However, if someone started the lockdown at 23:55:00 for 30 minutes, the "Time left to unlock" will jump to 21 hours, instead of 30 minutes. This only happens when a new day starts/timer starts before midnight.
java

I've got a couple of users in my app reporting a weird behavior of a time reset/bug, i was baffled by it and after testing and collecting logs - i figured that my timer, if started before midnight, resets to 20+ hours instantly, i've can't figure out why or how to prevent this.
The project is open source and anyone is welcome to help me with this, here are the links:
GitHub
Info about the app/project:
- Select apps, select desired lock-down time, lock apps.
My Timer Class ( Full )
Time/Date related snippet
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();
}
Any suggestions ?
EDIT:
- Timer works fine, if someone sets the lockdown for example, 30 minutes, it will countdown 30 minutes and unlock when it's done.
Let's say someone started the lockdown at 21:30:00, timer will work just fine and end at 10:00:00.
However, if someone started the lockdown at 23:55:00 for 30 minutes, the "Time left to unlock" will jump to 21 hours, instead of 30 minutes. This only happens when a new day starts/timer starts before midnight.
java

java

edited Jan 1 at 22:06
XerXes
asked Jan 1 at 21:34


XerXesXerXes
35
35
There is far too much code here for us to be able to answer this question. Please edit your question to include a Minimal, Complete, and Verifiable example (focus on "Minimal" and "Verifiable") that demonstrates your issue.
– Joe C
Jan 1 at 21:39
How are we supposed to know what's going on, when we don't know what values ofstrDate
anddb.get_Data(1)
are and/or are supposed to be?
– Andreas
Jan 1 at 21:41
@JoeC I deleted codes that are not related to the issue. Is that better?
– XerXes
Jan 1 at 21:46
@Andreas I just added the declared variables, is it enough? Or should i add more explanation ?
– XerXes
Jan 1 at 21:47
2
Basically, what we need is enough code (and no more) that we can copy and paste into our IDE of choice, click Run, and see the issue within a minute or two.
– Joe C
Jan 1 at 21:52
|
show 9 more comments
There is far too much code here for us to be able to answer this question. Please edit your question to include a Minimal, Complete, and Verifiable example (focus on "Minimal" and "Verifiable") that demonstrates your issue.
– Joe C
Jan 1 at 21:39
How are we supposed to know what's going on, when we don't know what values ofstrDate
anddb.get_Data(1)
are and/or are supposed to be?
– Andreas
Jan 1 at 21:41
@JoeC I deleted codes that are not related to the issue. Is that better?
– XerXes
Jan 1 at 21:46
@Andreas I just added the declared variables, is it enough? Or should i add more explanation ?
– XerXes
Jan 1 at 21:47
2
Basically, what we need is enough code (and no more) that we can copy and paste into our IDE of choice, click Run, and see the issue within a minute or two.
– Joe C
Jan 1 at 21:52
There is far too much code here for us to be able to answer this question. Please edit your question to include a Minimal, Complete, and Verifiable example (focus on "Minimal" and "Verifiable") that demonstrates your issue.
– Joe C
Jan 1 at 21:39
There is far too much code here for us to be able to answer this question. Please edit your question to include a Minimal, Complete, and Verifiable example (focus on "Minimal" and "Verifiable") that demonstrates your issue.
– Joe C
Jan 1 at 21:39
How are we supposed to know what's going on, when we don't know what values of
strDate
and db.get_Data(1)
are and/or are supposed to be?– Andreas
Jan 1 at 21:41
How are we supposed to know what's going on, when we don't know what values of
strDate
and db.get_Data(1)
are and/or are supposed to be?– Andreas
Jan 1 at 21:41
@JoeC I deleted codes that are not related to the issue. Is that better?
– XerXes
Jan 1 at 21:46
@JoeC I deleted codes that are not related to the issue. Is that better?
– XerXes
Jan 1 at 21:46
@Andreas I just added the declared variables, is it enough? Or should i add more explanation ?
– XerXes
Jan 1 at 21:47
@Andreas I just added the declared variables, is it enough? Or should i add more explanation ?
– XerXes
Jan 1 at 21:47
2
2
Basically, what we need is enough code (and no more) that we can copy and paste into our IDE of choice, click Run, and see the issue within a minute or two.
– Joe C
Jan 1 at 21:52
Basically, what we need is enough code (and no more) that we can copy and paste into our IDE of choice, click Run, and see the issue within a minute or two.
– Joe C
Jan 1 at 21:52
|
show 9 more comments
1 Answer
1
active
oldest
votes
The problem occurs because you have the time stored in "H:m:ss" format, but you are ignoring the date aspect. Using System.currentTimeMillis()
gives you the milliseconds after Jan 1 1970. So this includes the date aspect. Taking the difference of the two values will give you the milliseconds expired, which you can check against any time interval for the elapsed time.
Do this instead:
When saving the time for the timer should start use the millisecond instead of H:mm:ss :
long millisNow = System.currentTimeMillis();
save millisNow to the database.
And save the time that should elapse:
int minutes = 30;
long millisElapse = 30 * 60 * 1000;
save this to the database (or which ever time interval you need)
now get the difference
long diff = timeNow - timeDatabase;
if(diff > millisElapse){
//end session
}
BTW: The format you posted "H:M:ss". The capital "M" stands for month(!) not minutes.
Take a look at the documentation on the "Date and Time Patterns" SimpleDateFormat
Oh okay, so i'm actually taking the wrong difference of both dates, i am indeed saving my date in db as h:m:ss .. i'll try this and debug it tomorrow. Thank you so much for your information!
– XerXes
Jan 1 at 22:51
Even if you were to save the entire date asyyyyMMddHHmmss
the conversion usingnew SimpleDateFormat("H:mm:ss");
would still just return something like: 23:55:22 which has no reference to the date.
– Barns
Jan 1 at 22:59
I've implemented the code and tested it - it works on my device, but i'm waiting for a couple of users to test it as well and will report back. Thankyou so much for the help!
– XerXes
Jan 3 at 14:59
Glad it worked for you! Your profile has you located in Germany. I lived in and around Aachen for 20yrs. I just got tired of the rain! Moved back to the US.
– Barns
Jan 3 at 15:15
Oh really ? I go to Aachen almost every month, i got people there! Haha yeah the rain makes it terrible >.<
– XerXes
Jan 3 at 15:31
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999115%2fcountdown-timer-bug-after-midnight-related-to-timezone-and-clock%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem occurs because you have the time stored in "H:m:ss" format, but you are ignoring the date aspect. Using System.currentTimeMillis()
gives you the milliseconds after Jan 1 1970. So this includes the date aspect. Taking the difference of the two values will give you the milliseconds expired, which you can check against any time interval for the elapsed time.
Do this instead:
When saving the time for the timer should start use the millisecond instead of H:mm:ss :
long millisNow = System.currentTimeMillis();
save millisNow to the database.
And save the time that should elapse:
int minutes = 30;
long millisElapse = 30 * 60 * 1000;
save this to the database (or which ever time interval you need)
now get the difference
long diff = timeNow - timeDatabase;
if(diff > millisElapse){
//end session
}
BTW: The format you posted "H:M:ss". The capital "M" stands for month(!) not minutes.
Take a look at the documentation on the "Date and Time Patterns" SimpleDateFormat
Oh okay, so i'm actually taking the wrong difference of both dates, i am indeed saving my date in db as h:m:ss .. i'll try this and debug it tomorrow. Thank you so much for your information!
– XerXes
Jan 1 at 22:51
Even if you were to save the entire date asyyyyMMddHHmmss
the conversion usingnew SimpleDateFormat("H:mm:ss");
would still just return something like: 23:55:22 which has no reference to the date.
– Barns
Jan 1 at 22:59
I've implemented the code and tested it - it works on my device, but i'm waiting for a couple of users to test it as well and will report back. Thankyou so much for the help!
– XerXes
Jan 3 at 14:59
Glad it worked for you! Your profile has you located in Germany. I lived in and around Aachen for 20yrs. I just got tired of the rain! Moved back to the US.
– Barns
Jan 3 at 15:15
Oh really ? I go to Aachen almost every month, i got people there! Haha yeah the rain makes it terrible >.<
– XerXes
Jan 3 at 15:31
add a comment |
The problem occurs because you have the time stored in "H:m:ss" format, but you are ignoring the date aspect. Using System.currentTimeMillis()
gives you the milliseconds after Jan 1 1970. So this includes the date aspect. Taking the difference of the two values will give you the milliseconds expired, which you can check against any time interval for the elapsed time.
Do this instead:
When saving the time for the timer should start use the millisecond instead of H:mm:ss :
long millisNow = System.currentTimeMillis();
save millisNow to the database.
And save the time that should elapse:
int minutes = 30;
long millisElapse = 30 * 60 * 1000;
save this to the database (or which ever time interval you need)
now get the difference
long diff = timeNow - timeDatabase;
if(diff > millisElapse){
//end session
}
BTW: The format you posted "H:M:ss". The capital "M" stands for month(!) not minutes.
Take a look at the documentation on the "Date and Time Patterns" SimpleDateFormat
Oh okay, so i'm actually taking the wrong difference of both dates, i am indeed saving my date in db as h:m:ss .. i'll try this and debug it tomorrow. Thank you so much for your information!
– XerXes
Jan 1 at 22:51
Even if you were to save the entire date asyyyyMMddHHmmss
the conversion usingnew SimpleDateFormat("H:mm:ss");
would still just return something like: 23:55:22 which has no reference to the date.
– Barns
Jan 1 at 22:59
I've implemented the code and tested it - it works on my device, but i'm waiting for a couple of users to test it as well and will report back. Thankyou so much for the help!
– XerXes
Jan 3 at 14:59
Glad it worked for you! Your profile has you located in Germany. I lived in and around Aachen for 20yrs. I just got tired of the rain! Moved back to the US.
– Barns
Jan 3 at 15:15
Oh really ? I go to Aachen almost every month, i got people there! Haha yeah the rain makes it terrible >.<
– XerXes
Jan 3 at 15:31
add a comment |
The problem occurs because you have the time stored in "H:m:ss" format, but you are ignoring the date aspect. Using System.currentTimeMillis()
gives you the milliseconds after Jan 1 1970. So this includes the date aspect. Taking the difference of the two values will give you the milliseconds expired, which you can check against any time interval for the elapsed time.
Do this instead:
When saving the time for the timer should start use the millisecond instead of H:mm:ss :
long millisNow = System.currentTimeMillis();
save millisNow to the database.
And save the time that should elapse:
int minutes = 30;
long millisElapse = 30 * 60 * 1000;
save this to the database (or which ever time interval you need)
now get the difference
long diff = timeNow - timeDatabase;
if(diff > millisElapse){
//end session
}
BTW: The format you posted "H:M:ss". The capital "M" stands for month(!) not minutes.
Take a look at the documentation on the "Date and Time Patterns" SimpleDateFormat
The problem occurs because you have the time stored in "H:m:ss" format, but you are ignoring the date aspect. Using System.currentTimeMillis()
gives you the milliseconds after Jan 1 1970. So this includes the date aspect. Taking the difference of the two values will give you the milliseconds expired, which you can check against any time interval for the elapsed time.
Do this instead:
When saving the time for the timer should start use the millisecond instead of H:mm:ss :
long millisNow = System.currentTimeMillis();
save millisNow to the database.
And save the time that should elapse:
int minutes = 30;
long millisElapse = 30 * 60 * 1000;
save this to the database (or which ever time interval you need)
now get the difference
long diff = timeNow - timeDatabase;
if(diff > millisElapse){
//end session
}
BTW: The format you posted "H:M:ss". The capital "M" stands for month(!) not minutes.
Take a look at the documentation on the "Date and Time Patterns" SimpleDateFormat
edited Jan 1 at 23:02
answered Jan 1 at 22:22
BarnsBarns
3,5203726
3,5203726
Oh okay, so i'm actually taking the wrong difference of both dates, i am indeed saving my date in db as h:m:ss .. i'll try this and debug it tomorrow. Thank you so much for your information!
– XerXes
Jan 1 at 22:51
Even if you were to save the entire date asyyyyMMddHHmmss
the conversion usingnew SimpleDateFormat("H:mm:ss");
would still just return something like: 23:55:22 which has no reference to the date.
– Barns
Jan 1 at 22:59
I've implemented the code and tested it - it works on my device, but i'm waiting for a couple of users to test it as well and will report back. Thankyou so much for the help!
– XerXes
Jan 3 at 14:59
Glad it worked for you! Your profile has you located in Germany. I lived in and around Aachen for 20yrs. I just got tired of the rain! Moved back to the US.
– Barns
Jan 3 at 15:15
Oh really ? I go to Aachen almost every month, i got people there! Haha yeah the rain makes it terrible >.<
– XerXes
Jan 3 at 15:31
add a comment |
Oh okay, so i'm actually taking the wrong difference of both dates, i am indeed saving my date in db as h:m:ss .. i'll try this and debug it tomorrow. Thank you so much for your information!
– XerXes
Jan 1 at 22:51
Even if you were to save the entire date asyyyyMMddHHmmss
the conversion usingnew SimpleDateFormat("H:mm:ss");
would still just return something like: 23:55:22 which has no reference to the date.
– Barns
Jan 1 at 22:59
I've implemented the code and tested it - it works on my device, but i'm waiting for a couple of users to test it as well and will report back. Thankyou so much for the help!
– XerXes
Jan 3 at 14:59
Glad it worked for you! Your profile has you located in Germany. I lived in and around Aachen for 20yrs. I just got tired of the rain! Moved back to the US.
– Barns
Jan 3 at 15:15
Oh really ? I go to Aachen almost every month, i got people there! Haha yeah the rain makes it terrible >.<
– XerXes
Jan 3 at 15:31
Oh okay, so i'm actually taking the wrong difference of both dates, i am indeed saving my date in db as h:m:ss .. i'll try this and debug it tomorrow. Thank you so much for your information!
– XerXes
Jan 1 at 22:51
Oh okay, so i'm actually taking the wrong difference of both dates, i am indeed saving my date in db as h:m:ss .. i'll try this and debug it tomorrow. Thank you so much for your information!
– XerXes
Jan 1 at 22:51
Even if you were to save the entire date as
yyyyMMddHHmmss
the conversion using new SimpleDateFormat("H:mm:ss");
would still just return something like: 23:55:22 which has no reference to the date.– Barns
Jan 1 at 22:59
Even if you were to save the entire date as
yyyyMMddHHmmss
the conversion using new SimpleDateFormat("H:mm:ss");
would still just return something like: 23:55:22 which has no reference to the date.– Barns
Jan 1 at 22:59
I've implemented the code and tested it - it works on my device, but i'm waiting for a couple of users to test it as well and will report back. Thankyou so much for the help!
– XerXes
Jan 3 at 14:59
I've implemented the code and tested it - it works on my device, but i'm waiting for a couple of users to test it as well and will report back. Thankyou so much for the help!
– XerXes
Jan 3 at 14:59
Glad it worked for you! Your profile has you located in Germany. I lived in and around Aachen for 20yrs. I just got tired of the rain! Moved back to the US.
– Barns
Jan 3 at 15:15
Glad it worked for you! Your profile has you located in Germany. I lived in and around Aachen for 20yrs. I just got tired of the rain! Moved back to the US.
– Barns
Jan 3 at 15:15
Oh really ? I go to Aachen almost every month, i got people there! Haha yeah the rain makes it terrible >.<
– XerXes
Jan 3 at 15:31
Oh really ? I go to Aachen almost every month, i got people there! Haha yeah the rain makes it terrible >.<
– XerXes
Jan 3 at 15:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999115%2fcountdown-timer-bug-after-midnight-related-to-timezone-and-clock%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There is far too much code here for us to be able to answer this question. Please edit your question to include a Minimal, Complete, and Verifiable example (focus on "Minimal" and "Verifiable") that demonstrates your issue.
– Joe C
Jan 1 at 21:39
How are we supposed to know what's going on, when we don't know what values of
strDate
anddb.get_Data(1)
are and/or are supposed to be?– Andreas
Jan 1 at 21:41
@JoeC I deleted codes that are not related to the issue. Is that better?
– XerXes
Jan 1 at 21:46
@Andreas I just added the declared variables, is it enough? Or should i add more explanation ?
– XerXes
Jan 1 at 21:47
2
Basically, what we need is enough code (and no more) that we can copy and paste into our IDE of choice, click Run, and see the issue within a minute or two.
– Joe C
Jan 1 at 21:52