Make timer count from Date
up vote
0
down vote
favorite
I am trying to make a Timer that will start counting from Date,
so every time i launch the app, the Timer will always be updated
for example if i start the timer at 20:00 22/11/18, tomorrow at 21:00 it will show 25:00:00.
I have only found how to do a CountdownTimer, or just a simple timer.
java android
add a comment |
up vote
0
down vote
favorite
I am trying to make a Timer that will start counting from Date,
so every time i launch the app, the Timer will always be updated
for example if i start the timer at 20:00 22/11/18, tomorrow at 21:00 it will show 25:00:00.
I have only found how to do a CountdownTimer, or just a simple timer.
java android
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to make a Timer that will start counting from Date,
so every time i launch the app, the Timer will always be updated
for example if i start the timer at 20:00 22/11/18, tomorrow at 21:00 it will show 25:00:00.
I have only found how to do a CountdownTimer, or just a simple timer.
java android
I am trying to make a Timer that will start counting from Date,
so every time i launch the app, the Timer will always be updated
for example if i start the timer at 20:00 22/11/18, tomorrow at 21:00 it will show 25:00:00.
I have only found how to do a CountdownTimer, or just a simple timer.
java android
java android
asked Nov 22 at 15:36
Dimon
13
13
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
You can get the current time when you start the timer with:
long timerStart = System.currentTimeMillis();
And then when you want to show what the timer is at calculate it by doing
long timePassed = System.currentTimeMillis() - timerStart;
And that will give you the number of milliseconds since you started the timer. And to format it the way you want you can pass it into this function:
public static String convertMillisToHMmSs(long millis) {
long seconds = millis / 1000
long s = seconds % 60;
long m = (seconds / 60) % 60;
long h = (seconds / (60 * 60));
return String.format("%d:%02d:%02d", h,m,s);
}
Edit: As mentioned by the other answers, you will need to store the timerStart somewhere to keep track of it after the app is closed/reopened. I would recommend something like shared preferences you can look at this question to figure out how to do that
add a comment |
up vote
0
down vote
Unless you are willing to create an app that will run in background the whole time for few days (which would be highly unoptimized for an app of this complexity)
I think the best solution would be to store your start date (start timestamp) somewhere. Either in Room or in Shared Preferences and not to program your APP to increase or decrease your counter by one every second, than rather to calculate difference between start and current timestamp every second.
There are obviously a lot questions about performance but according to your question I guess that you are not concerned by this, and it will be a good practice for you to optimize this solution to be faster and more precise.
add a comment |
up vote
0
down vote
Agree above with Quinn however, somewhere you need to create a file that stores the current time. Otherwise every time app restarts the variable timerStart
will reset.
So you need to create a file that stores the 'timerStart' so that every time you start, it updates from the value.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can get the current time when you start the timer with:
long timerStart = System.currentTimeMillis();
And then when you want to show what the timer is at calculate it by doing
long timePassed = System.currentTimeMillis() - timerStart;
And that will give you the number of milliseconds since you started the timer. And to format it the way you want you can pass it into this function:
public static String convertMillisToHMmSs(long millis) {
long seconds = millis / 1000
long s = seconds % 60;
long m = (seconds / 60) % 60;
long h = (seconds / (60 * 60));
return String.format("%d:%02d:%02d", h,m,s);
}
Edit: As mentioned by the other answers, you will need to store the timerStart somewhere to keep track of it after the app is closed/reopened. I would recommend something like shared preferences you can look at this question to figure out how to do that
add a comment |
up vote
1
down vote
You can get the current time when you start the timer with:
long timerStart = System.currentTimeMillis();
And then when you want to show what the timer is at calculate it by doing
long timePassed = System.currentTimeMillis() - timerStart;
And that will give you the number of milliseconds since you started the timer. And to format it the way you want you can pass it into this function:
public static String convertMillisToHMmSs(long millis) {
long seconds = millis / 1000
long s = seconds % 60;
long m = (seconds / 60) % 60;
long h = (seconds / (60 * 60));
return String.format("%d:%02d:%02d", h,m,s);
}
Edit: As mentioned by the other answers, you will need to store the timerStart somewhere to keep track of it after the app is closed/reopened. I would recommend something like shared preferences you can look at this question to figure out how to do that
add a comment |
up vote
1
down vote
up vote
1
down vote
You can get the current time when you start the timer with:
long timerStart = System.currentTimeMillis();
And then when you want to show what the timer is at calculate it by doing
long timePassed = System.currentTimeMillis() - timerStart;
And that will give you the number of milliseconds since you started the timer. And to format it the way you want you can pass it into this function:
public static String convertMillisToHMmSs(long millis) {
long seconds = millis / 1000
long s = seconds % 60;
long m = (seconds / 60) % 60;
long h = (seconds / (60 * 60));
return String.format("%d:%02d:%02d", h,m,s);
}
Edit: As mentioned by the other answers, you will need to store the timerStart somewhere to keep track of it after the app is closed/reopened. I would recommend something like shared preferences you can look at this question to figure out how to do that
You can get the current time when you start the timer with:
long timerStart = System.currentTimeMillis();
And then when you want to show what the timer is at calculate it by doing
long timePassed = System.currentTimeMillis() - timerStart;
And that will give you the number of milliseconds since you started the timer. And to format it the way you want you can pass it into this function:
public static String convertMillisToHMmSs(long millis) {
long seconds = millis / 1000
long s = seconds % 60;
long m = (seconds / 60) % 60;
long h = (seconds / (60 * 60));
return String.format("%d:%02d:%02d", h,m,s);
}
Edit: As mentioned by the other answers, you will need to store the timerStart somewhere to keep track of it after the app is closed/reopened. I would recommend something like shared preferences you can look at this question to figure out how to do that
edited Nov 22 at 16:12
answered Nov 22 at 15:45
Quinn
3628
3628
add a comment |
add a comment |
up vote
0
down vote
Unless you are willing to create an app that will run in background the whole time for few days (which would be highly unoptimized for an app of this complexity)
I think the best solution would be to store your start date (start timestamp) somewhere. Either in Room or in Shared Preferences and not to program your APP to increase or decrease your counter by one every second, than rather to calculate difference between start and current timestamp every second.
There are obviously a lot questions about performance but according to your question I guess that you are not concerned by this, and it will be a good practice for you to optimize this solution to be faster and more precise.
add a comment |
up vote
0
down vote
Unless you are willing to create an app that will run in background the whole time for few days (which would be highly unoptimized for an app of this complexity)
I think the best solution would be to store your start date (start timestamp) somewhere. Either in Room or in Shared Preferences and not to program your APP to increase or decrease your counter by one every second, than rather to calculate difference between start and current timestamp every second.
There are obviously a lot questions about performance but according to your question I guess that you are not concerned by this, and it will be a good practice for you to optimize this solution to be faster and more precise.
add a comment |
up vote
0
down vote
up vote
0
down vote
Unless you are willing to create an app that will run in background the whole time for few days (which would be highly unoptimized for an app of this complexity)
I think the best solution would be to store your start date (start timestamp) somewhere. Either in Room or in Shared Preferences and not to program your APP to increase or decrease your counter by one every second, than rather to calculate difference between start and current timestamp every second.
There are obviously a lot questions about performance but according to your question I guess that you are not concerned by this, and it will be a good practice for you to optimize this solution to be faster and more precise.
Unless you are willing to create an app that will run in background the whole time for few days (which would be highly unoptimized for an app of this complexity)
I think the best solution would be to store your start date (start timestamp) somewhere. Either in Room or in Shared Preferences and not to program your APP to increase or decrease your counter by one every second, than rather to calculate difference between start and current timestamp every second.
There are obviously a lot questions about performance but according to your question I guess that you are not concerned by this, and it will be a good practice for you to optimize this solution to be faster and more precise.
answered Nov 22 at 15:53
ralic
206
206
add a comment |
add a comment |
up vote
0
down vote
Agree above with Quinn however, somewhere you need to create a file that stores the current time. Otherwise every time app restarts the variable timerStart
will reset.
So you need to create a file that stores the 'timerStart' so that every time you start, it updates from the value.
add a comment |
up vote
0
down vote
Agree above with Quinn however, somewhere you need to create a file that stores the current time. Otherwise every time app restarts the variable timerStart
will reset.
So you need to create a file that stores the 'timerStart' so that every time you start, it updates from the value.
add a comment |
up vote
0
down vote
up vote
0
down vote
Agree above with Quinn however, somewhere you need to create a file that stores the current time. Otherwise every time app restarts the variable timerStart
will reset.
So you need to create a file that stores the 'timerStart' so that every time you start, it updates from the value.
Agree above with Quinn however, somewhere you need to create a file that stores the current time. Otherwise every time app restarts the variable timerStart
will reset.
So you need to create a file that stores the 'timerStart' so that every time you start, it updates from the value.
answered Nov 22 at 16:06
Adeel Malik
1
1
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53434240%2fmake-timer-count-from-date%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