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.










share|improve this question


























    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.










    share|improve this question
























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 15:36









      Dimon

      13




      13
























          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






          share|improve this answer






























            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.






            share|improve this answer




























              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.






              share|improve this answer





















                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',
                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
                });


                }
                });














                draft saved

                draft discarded


















                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

























                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






                share|improve this answer



























                  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






                  share|improve this answer

























                    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






                    share|improve this answer














                    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







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 22 at 16:12

























                    answered Nov 22 at 15:45









                    Quinn

                    3628




                    3628
























                        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.






                        share|improve this answer

























                          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.






                          share|improve this answer























                            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.






                            share|improve this answer












                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 22 at 15:53









                            ralic

                            206




                            206






















                                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.






                                share|improve this answer

























                                  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.






                                  share|improve this answer























                                    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.






                                    share|improve this answer












                                    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.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 22 at 16:06









                                    Adeel Malik

                                    1




                                    1






























                                        draft saved

                                        draft discarded




















































                                        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.




                                        draft saved


                                        draft discarded














                                        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





















































                                        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







                                        Popular posts from this blog

                                        What visual should I use to simply compare current year value vs last year in Power BI desktop

                                        How to ignore python UserWarning in pytest?

                                        Alexandru Averescu