Create a boolean that resets it's value at midnight?
Is there a simple way to create a boolean that resets its value to false at midnight?
I thought in using an if statment like
Calendar now = Calendar.getInstance();
Calendar midnight = Calendar.getInstance();
midnight.add(Calendar.HOUR_OF_DAY, 23);
midnight.add(Calendar.MINUTE,59);
midnight.add(Calendar.SECOND, 59);
if (now == midnight){
boolean = false;
}
But it only works if the activity is running and I need it to run even if the app is closed
I know I can do it using the AlarmManager and the Shared Preferences but I was wondering if there is a more obvious an easy way that I've not thought off.
java
add a comment |
Is there a simple way to create a boolean that resets its value to false at midnight?
I thought in using an if statment like
Calendar now = Calendar.getInstance();
Calendar midnight = Calendar.getInstance();
midnight.add(Calendar.HOUR_OF_DAY, 23);
midnight.add(Calendar.MINUTE,59);
midnight.add(Calendar.SECOND, 59);
if (now == midnight){
boolean = false;
}
But it only works if the activity is running and I need it to run even if the app is closed
I know I can do it using the AlarmManager and the Shared Preferences but I was wondering if there is a more obvious an easy way that I've not thought off.
java
why do you need that
– Tim Castelijns
Nov 24 '18 at 18:09
Use intentService or job scheduler for this kind of task
– Show Young Soyinka
Nov 24 '18 at 18:21
I want to keep track if the user didn't click a button. This button onClick changes the boolean to true. And at the end of the day if the boolean is false (Button wasn't clicked) I'll add one increment to the missed clicks progress bar. And if it returns true I'll add one increment to the clicks progress bar. This is why I need the boolean to turn itself back to false at midnight so the next day the code repeats itself.
– newbieCoder.pkg
Nov 24 '18 at 18:23
add a comment |
Is there a simple way to create a boolean that resets its value to false at midnight?
I thought in using an if statment like
Calendar now = Calendar.getInstance();
Calendar midnight = Calendar.getInstance();
midnight.add(Calendar.HOUR_OF_DAY, 23);
midnight.add(Calendar.MINUTE,59);
midnight.add(Calendar.SECOND, 59);
if (now == midnight){
boolean = false;
}
But it only works if the activity is running and I need it to run even if the app is closed
I know I can do it using the AlarmManager and the Shared Preferences but I was wondering if there is a more obvious an easy way that I've not thought off.
java
Is there a simple way to create a boolean that resets its value to false at midnight?
I thought in using an if statment like
Calendar now = Calendar.getInstance();
Calendar midnight = Calendar.getInstance();
midnight.add(Calendar.HOUR_OF_DAY, 23);
midnight.add(Calendar.MINUTE,59);
midnight.add(Calendar.SECOND, 59);
if (now == midnight){
boolean = false;
}
But it only works if the activity is running and I need it to run even if the app is closed
I know I can do it using the AlarmManager and the Shared Preferences but I was wondering if there is a more obvious an easy way that I've not thought off.
java
java
asked Nov 24 '18 at 18:06
newbieCoder.pkgnewbieCoder.pkg
15013
15013
why do you need that
– Tim Castelijns
Nov 24 '18 at 18:09
Use intentService or job scheduler for this kind of task
– Show Young Soyinka
Nov 24 '18 at 18:21
I want to keep track if the user didn't click a button. This button onClick changes the boolean to true. And at the end of the day if the boolean is false (Button wasn't clicked) I'll add one increment to the missed clicks progress bar. And if it returns true I'll add one increment to the clicks progress bar. This is why I need the boolean to turn itself back to false at midnight so the next day the code repeats itself.
– newbieCoder.pkg
Nov 24 '18 at 18:23
add a comment |
why do you need that
– Tim Castelijns
Nov 24 '18 at 18:09
Use intentService or job scheduler for this kind of task
– Show Young Soyinka
Nov 24 '18 at 18:21
I want to keep track if the user didn't click a button. This button onClick changes the boolean to true. And at the end of the day if the boolean is false (Button wasn't clicked) I'll add one increment to the missed clicks progress bar. And if it returns true I'll add one increment to the clicks progress bar. This is why I need the boolean to turn itself back to false at midnight so the next day the code repeats itself.
– newbieCoder.pkg
Nov 24 '18 at 18:23
why do you need that
– Tim Castelijns
Nov 24 '18 at 18:09
why do you need that
– Tim Castelijns
Nov 24 '18 at 18:09
Use intentService or job scheduler for this kind of task
– Show Young Soyinka
Nov 24 '18 at 18:21
Use intentService or job scheduler for this kind of task
– Show Young Soyinka
Nov 24 '18 at 18:21
I want to keep track if the user didn't click a button. This button onClick changes the boolean to true. And at the end of the day if the boolean is false (Button wasn't clicked) I'll add one increment to the missed clicks progress bar. And if it returns true I'll add one increment to the clicks progress bar. This is why I need the boolean to turn itself back to false at midnight so the next day the code repeats itself.
– newbieCoder.pkg
Nov 24 '18 at 18:23
I want to keep track if the user didn't click a button. This button onClick changes the boolean to true. And at the end of the day if the boolean is false (Button wasn't clicked) I'll add one increment to the missed clicks progress bar. And if it returns true I'll add one increment to the clicks progress bar. This is why I need the boolean to turn itself back to false at midnight so the next day the code repeats itself.
– newbieCoder.pkg
Nov 24 '18 at 18:23
add a comment |
1 Answer
1
active
oldest
votes
You could simply create a kind of "Midnight Resetting Boolean" class for this. Get the value every time the Activity is loaded and increment the counter by one if it's false.
package com.bemoty.booleantest;
import java.util.Calendar;
import java.util.Date;
public class MidnightBoolean {
private boolean value = true;
private long lastChanged = 0L;
public MidnightBoolean(boolean startValue) {
this.setValue(startValue);
}
public void setValue(boolean value) {
this.value = value;
this.lastChanged = System.currentTimeMillis();
}
public boolean getValue() {
Calendar lastChangedCalendar = Calendar.getInstance();
Calendar timeNowCalendar = Calendar.getInstance();
lastChangedCalendar.setTime(new Date(lastChanged));
if ((lastChangedCalendar.get(Calendar.DAY_OF_MONTH) - timeNowCalendar.get(Calendar.DAY_OF_MONTH)) < 0) {
this.setValue(false);
}
return this.value;
}
}
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%2f53461006%2fcreate-a-boolean-that-resets-its-value-at-midnight%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
You could simply create a kind of "Midnight Resetting Boolean" class for this. Get the value every time the Activity is loaded and increment the counter by one if it's false.
package com.bemoty.booleantest;
import java.util.Calendar;
import java.util.Date;
public class MidnightBoolean {
private boolean value = true;
private long lastChanged = 0L;
public MidnightBoolean(boolean startValue) {
this.setValue(startValue);
}
public void setValue(boolean value) {
this.value = value;
this.lastChanged = System.currentTimeMillis();
}
public boolean getValue() {
Calendar lastChangedCalendar = Calendar.getInstance();
Calendar timeNowCalendar = Calendar.getInstance();
lastChangedCalendar.setTime(new Date(lastChanged));
if ((lastChangedCalendar.get(Calendar.DAY_OF_MONTH) - timeNowCalendar.get(Calendar.DAY_OF_MONTH)) < 0) {
this.setValue(false);
}
return this.value;
}
}
add a comment |
You could simply create a kind of "Midnight Resetting Boolean" class for this. Get the value every time the Activity is loaded and increment the counter by one if it's false.
package com.bemoty.booleantest;
import java.util.Calendar;
import java.util.Date;
public class MidnightBoolean {
private boolean value = true;
private long lastChanged = 0L;
public MidnightBoolean(boolean startValue) {
this.setValue(startValue);
}
public void setValue(boolean value) {
this.value = value;
this.lastChanged = System.currentTimeMillis();
}
public boolean getValue() {
Calendar lastChangedCalendar = Calendar.getInstance();
Calendar timeNowCalendar = Calendar.getInstance();
lastChangedCalendar.setTime(new Date(lastChanged));
if ((lastChangedCalendar.get(Calendar.DAY_OF_MONTH) - timeNowCalendar.get(Calendar.DAY_OF_MONTH)) < 0) {
this.setValue(false);
}
return this.value;
}
}
add a comment |
You could simply create a kind of "Midnight Resetting Boolean" class for this. Get the value every time the Activity is loaded and increment the counter by one if it's false.
package com.bemoty.booleantest;
import java.util.Calendar;
import java.util.Date;
public class MidnightBoolean {
private boolean value = true;
private long lastChanged = 0L;
public MidnightBoolean(boolean startValue) {
this.setValue(startValue);
}
public void setValue(boolean value) {
this.value = value;
this.lastChanged = System.currentTimeMillis();
}
public boolean getValue() {
Calendar lastChangedCalendar = Calendar.getInstance();
Calendar timeNowCalendar = Calendar.getInstance();
lastChangedCalendar.setTime(new Date(lastChanged));
if ((lastChangedCalendar.get(Calendar.DAY_OF_MONTH) - timeNowCalendar.get(Calendar.DAY_OF_MONTH)) < 0) {
this.setValue(false);
}
return this.value;
}
}
You could simply create a kind of "Midnight Resetting Boolean" class for this. Get the value every time the Activity is loaded and increment the counter by one if it's false.
package com.bemoty.booleantest;
import java.util.Calendar;
import java.util.Date;
public class MidnightBoolean {
private boolean value = true;
private long lastChanged = 0L;
public MidnightBoolean(boolean startValue) {
this.setValue(startValue);
}
public void setValue(boolean value) {
this.value = value;
this.lastChanged = System.currentTimeMillis();
}
public boolean getValue() {
Calendar lastChangedCalendar = Calendar.getInstance();
Calendar timeNowCalendar = Calendar.getInstance();
lastChangedCalendar.setTime(new Date(lastChanged));
if ((lastChangedCalendar.get(Calendar.DAY_OF_MONTH) - timeNowCalendar.get(Calendar.DAY_OF_MONTH)) < 0) {
this.setValue(false);
}
return this.value;
}
}
answered Nov 24 '18 at 18:29
BemotyBemoty
12
12
add a comment |
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%2f53461006%2fcreate-a-boolean-that-resets-its-value-at-midnight%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
why do you need that
– Tim Castelijns
Nov 24 '18 at 18:09
Use intentService or job scheduler for this kind of task
– Show Young Soyinka
Nov 24 '18 at 18:21
I want to keep track if the user didn't click a button. This button onClick changes the boolean to true. And at the end of the day if the boolean is false (Button wasn't clicked) I'll add one increment to the missed clicks progress bar. And if it returns true I'll add one increment to the clicks progress bar. This is why I need the boolean to turn itself back to false at midnight so the next day the code repeats itself.
– newbieCoder.pkg
Nov 24 '18 at 18:23