Checking whether a timestamp is 10 minutes old











up vote
4
down vote

favorite
2












I have a timestamp (searchTimestamp) which I need to check to see whether it is less than 10 minutes old or not:



long currentTimestamp = System.currentTimeMillis();

long searchTimestamp = getTheTimestampFromURL();// this also gives me back timestamp in 13 digit (1425506040493)

long difference = Math.abs(currentTimestamp - searchTimestamp);

System.out.println(difference);

if (difference > 10 * 60 * 1000) {
System.out.println("timestamp is greater than 5 minutes old");
}


I have got the above code which is working fine. Is this the right way to do this or is there any better way?



getTheTimestampFromURL will always be older than the current timestamp in milliseconds.










share|improve this question




























    up vote
    4
    down vote

    favorite
    2












    I have a timestamp (searchTimestamp) which I need to check to see whether it is less than 10 minutes old or not:



    long currentTimestamp = System.currentTimeMillis();

    long searchTimestamp = getTheTimestampFromURL();// this also gives me back timestamp in 13 digit (1425506040493)

    long difference = Math.abs(currentTimestamp - searchTimestamp);

    System.out.println(difference);

    if (difference > 10 * 60 * 1000) {
    System.out.println("timestamp is greater than 5 minutes old");
    }


    I have got the above code which is working fine. Is this the right way to do this or is there any better way?



    getTheTimestampFromURL will always be older than the current timestamp in milliseconds.










    share|improve this question


























      up vote
      4
      down vote

      favorite
      2









      up vote
      4
      down vote

      favorite
      2






      2





      I have a timestamp (searchTimestamp) which I need to check to see whether it is less than 10 minutes old or not:



      long currentTimestamp = System.currentTimeMillis();

      long searchTimestamp = getTheTimestampFromURL();// this also gives me back timestamp in 13 digit (1425506040493)

      long difference = Math.abs(currentTimestamp - searchTimestamp);

      System.out.println(difference);

      if (difference > 10 * 60 * 1000) {
      System.out.println("timestamp is greater than 5 minutes old");
      }


      I have got the above code which is working fine. Is this the right way to do this or is there any better way?



      getTheTimestampFromURL will always be older than the current timestamp in milliseconds.










      share|improve this question















      I have a timestamp (searchTimestamp) which I need to check to see whether it is less than 10 minutes old or not:



      long currentTimestamp = System.currentTimeMillis();

      long searchTimestamp = getTheTimestampFromURL();// this also gives me back timestamp in 13 digit (1425506040493)

      long difference = Math.abs(currentTimestamp - searchTimestamp);

      System.out.println(difference);

      if (difference > 10 * 60 * 1000) {
      System.out.println("timestamp is greater than 5 minutes old");
      }


      I have got the above code which is working fine. Is this the right way to do this or is there any better way?



      getTheTimestampFromURL will always be older than the current timestamp in milliseconds.







      java datetime






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 5 '15 at 18:14









      200_success

      127k15148411




      127k15148411










      asked Mar 4 '15 at 23:58









      david

      44252137




      44252137






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          11
          down vote



          accepted










          Your description says that you want to check whether the searchTimestamp is less than 10 minutes old. Your code does something different, though.



          Your code checks whether there's less than 10 minutes between the times (the difference is <= 10 minutes .... If the searchTimestamp is 2 minutes in the future, it will pass the test, if it is 9 minutes in the future, it will pass the test, and if it is 11 minutes in the future, it will fail the test.



          Of interest, the math you use does a 10 minute check, but the message says "greater than 5 minutes old."



          Your message should say: "timestamp is older than 10 minutes, or more than 10 minutes in the future"



          So, changing your code to be what I think it should be, is a lot simpler than you would think.



          What you want is for the searchTimestamp to have happened sometime after 10 minutes ago.... this is the way to do it:



          private static final int TEN_MINUTES = 10 * 60 * 1000;


          then, in your method:



          long tenAgo = System.currentTimeMillis() - TEN_MINUTES;
          if (searchTimestamp < tenAgo) {
          System.out.println("searchTimestamp is older than 10 minutes");
          }





          share|improve this answer






























            up vote
            4
            down vote













            joda-time is a very nice library that can overcome some of the shortfalls in the core Java Date/Time classes. For example:



            DateTime now = new DateTime();

            DateTime fromString = DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(input);
            DateTime fromDate = new DateTime(timeInMillis);
            DateTime then = fromDate;

            Interval interval = new Interval(then, now);
            if (interval.toDuration().getStandardMinutes() > 10) {
            ...
            }


            I wouldn't use Math.abs(...) to bypass not knowing which way around to compare your timestamps. That's just lazy.



            Also, 10, 60, etc. are magic numbers relating to time. joda-time has DateTimeConstants.MILLIS_PER_MINUTE if you must have the number, but for manipulating DateTimes you should be using functions rather than directly manipulating the millis. Core Java, you can use TimeUnit.MINUTES.toMillis(10).






            share|improve this answer




























              up vote
              -1
              down vote













              To check if date is today, use Android utils library




              DateUtils.isToday(long timeInMilliseconds)




              This utils class also offers human readable strings for relative times. For example,




              DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42
              minutes ago"




              The are several parameters you can play with to define how precise the time span should be






              share|improve this answer

















              • 2




                Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
                – Calak
                Nov 20 at 10:34











              Your Answer





              StackExchange.ifUsing("editor", function () {
              return StackExchange.using("mathjaxEditing", function () {
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              });
              });
              }, "mathjax-editing");

              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: "196"
              };
              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: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2fcodereview.stackexchange.com%2fquestions%2f83244%2fchecking-whether-a-timestamp-is-10-minutes-old%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
              11
              down vote



              accepted










              Your description says that you want to check whether the searchTimestamp is less than 10 minutes old. Your code does something different, though.



              Your code checks whether there's less than 10 minutes between the times (the difference is <= 10 minutes .... If the searchTimestamp is 2 minutes in the future, it will pass the test, if it is 9 minutes in the future, it will pass the test, and if it is 11 minutes in the future, it will fail the test.



              Of interest, the math you use does a 10 minute check, but the message says "greater than 5 minutes old."



              Your message should say: "timestamp is older than 10 minutes, or more than 10 minutes in the future"



              So, changing your code to be what I think it should be, is a lot simpler than you would think.



              What you want is for the searchTimestamp to have happened sometime after 10 minutes ago.... this is the way to do it:



              private static final int TEN_MINUTES = 10 * 60 * 1000;


              then, in your method:



              long tenAgo = System.currentTimeMillis() - TEN_MINUTES;
              if (searchTimestamp < tenAgo) {
              System.out.println("searchTimestamp is older than 10 minutes");
              }





              share|improve this answer



























                up vote
                11
                down vote



                accepted










                Your description says that you want to check whether the searchTimestamp is less than 10 minutes old. Your code does something different, though.



                Your code checks whether there's less than 10 minutes between the times (the difference is <= 10 minutes .... If the searchTimestamp is 2 minutes in the future, it will pass the test, if it is 9 minutes in the future, it will pass the test, and if it is 11 minutes in the future, it will fail the test.



                Of interest, the math you use does a 10 minute check, but the message says "greater than 5 minutes old."



                Your message should say: "timestamp is older than 10 minutes, or more than 10 minutes in the future"



                So, changing your code to be what I think it should be, is a lot simpler than you would think.



                What you want is for the searchTimestamp to have happened sometime after 10 minutes ago.... this is the way to do it:



                private static final int TEN_MINUTES = 10 * 60 * 1000;


                then, in your method:



                long tenAgo = System.currentTimeMillis() - TEN_MINUTES;
                if (searchTimestamp < tenAgo) {
                System.out.println("searchTimestamp is older than 10 minutes");
                }





                share|improve this answer

























                  up vote
                  11
                  down vote



                  accepted







                  up vote
                  11
                  down vote



                  accepted






                  Your description says that you want to check whether the searchTimestamp is less than 10 minutes old. Your code does something different, though.



                  Your code checks whether there's less than 10 minutes between the times (the difference is <= 10 minutes .... If the searchTimestamp is 2 minutes in the future, it will pass the test, if it is 9 minutes in the future, it will pass the test, and if it is 11 minutes in the future, it will fail the test.



                  Of interest, the math you use does a 10 minute check, but the message says "greater than 5 minutes old."



                  Your message should say: "timestamp is older than 10 minutes, or more than 10 minutes in the future"



                  So, changing your code to be what I think it should be, is a lot simpler than you would think.



                  What you want is for the searchTimestamp to have happened sometime after 10 minutes ago.... this is the way to do it:



                  private static final int TEN_MINUTES = 10 * 60 * 1000;


                  then, in your method:



                  long tenAgo = System.currentTimeMillis() - TEN_MINUTES;
                  if (searchTimestamp < tenAgo) {
                  System.out.println("searchTimestamp is older than 10 minutes");
                  }





                  share|improve this answer














                  Your description says that you want to check whether the searchTimestamp is less than 10 minutes old. Your code does something different, though.



                  Your code checks whether there's less than 10 minutes between the times (the difference is <= 10 minutes .... If the searchTimestamp is 2 minutes in the future, it will pass the test, if it is 9 minutes in the future, it will pass the test, and if it is 11 minutes in the future, it will fail the test.



                  Of interest, the math you use does a 10 minute check, but the message says "greater than 5 minutes old."



                  Your message should say: "timestamp is older than 10 minutes, or more than 10 minutes in the future"



                  So, changing your code to be what I think it should be, is a lot simpler than you would think.



                  What you want is for the searchTimestamp to have happened sometime after 10 minutes ago.... this is the way to do it:



                  private static final int TEN_MINUTES = 10 * 60 * 1000;


                  then, in your method:



                  long tenAgo = System.currentTimeMillis() - TEN_MINUTES;
                  if (searchTimestamp < tenAgo) {
                  System.out.println("searchTimestamp is older than 10 minutes");
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 9 at 23:10

























                  answered Mar 5 '15 at 1:28









                  rolfl

                  90.6k13190393




                  90.6k13190393
























                      up vote
                      4
                      down vote













                      joda-time is a very nice library that can overcome some of the shortfalls in the core Java Date/Time classes. For example:



                      DateTime now = new DateTime();

                      DateTime fromString = DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(input);
                      DateTime fromDate = new DateTime(timeInMillis);
                      DateTime then = fromDate;

                      Interval interval = new Interval(then, now);
                      if (interval.toDuration().getStandardMinutes() > 10) {
                      ...
                      }


                      I wouldn't use Math.abs(...) to bypass not knowing which way around to compare your timestamps. That's just lazy.



                      Also, 10, 60, etc. are magic numbers relating to time. joda-time has DateTimeConstants.MILLIS_PER_MINUTE if you must have the number, but for manipulating DateTimes you should be using functions rather than directly manipulating the millis. Core Java, you can use TimeUnit.MINUTES.toMillis(10).






                      share|improve this answer

























                        up vote
                        4
                        down vote













                        joda-time is a very nice library that can overcome some of the shortfalls in the core Java Date/Time classes. For example:



                        DateTime now = new DateTime();

                        DateTime fromString = DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(input);
                        DateTime fromDate = new DateTime(timeInMillis);
                        DateTime then = fromDate;

                        Interval interval = new Interval(then, now);
                        if (interval.toDuration().getStandardMinutes() > 10) {
                        ...
                        }


                        I wouldn't use Math.abs(...) to bypass not knowing which way around to compare your timestamps. That's just lazy.



                        Also, 10, 60, etc. are magic numbers relating to time. joda-time has DateTimeConstants.MILLIS_PER_MINUTE if you must have the number, but for manipulating DateTimes you should be using functions rather than directly manipulating the millis. Core Java, you can use TimeUnit.MINUTES.toMillis(10).






                        share|improve this answer























                          up vote
                          4
                          down vote










                          up vote
                          4
                          down vote









                          joda-time is a very nice library that can overcome some of the shortfalls in the core Java Date/Time classes. For example:



                          DateTime now = new DateTime();

                          DateTime fromString = DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(input);
                          DateTime fromDate = new DateTime(timeInMillis);
                          DateTime then = fromDate;

                          Interval interval = new Interval(then, now);
                          if (interval.toDuration().getStandardMinutes() > 10) {
                          ...
                          }


                          I wouldn't use Math.abs(...) to bypass not knowing which way around to compare your timestamps. That's just lazy.



                          Also, 10, 60, etc. are magic numbers relating to time. joda-time has DateTimeConstants.MILLIS_PER_MINUTE if you must have the number, but for manipulating DateTimes you should be using functions rather than directly manipulating the millis. Core Java, you can use TimeUnit.MINUTES.toMillis(10).






                          share|improve this answer












                          joda-time is a very nice library that can overcome some of the shortfalls in the core Java Date/Time classes. For example:



                          DateTime now = new DateTime();

                          DateTime fromString = DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(input);
                          DateTime fromDate = new DateTime(timeInMillis);
                          DateTime then = fromDate;

                          Interval interval = new Interval(then, now);
                          if (interval.toDuration().getStandardMinutes() > 10) {
                          ...
                          }


                          I wouldn't use Math.abs(...) to bypass not knowing which way around to compare your timestamps. That's just lazy.



                          Also, 10, 60, etc. are magic numbers relating to time. joda-time has DateTimeConstants.MILLIS_PER_MINUTE if you must have the number, but for manipulating DateTimes you should be using functions rather than directly manipulating the millis. Core Java, you can use TimeUnit.MINUTES.toMillis(10).







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 5 '15 at 11:28









                          Danikov

                          51725




                          51725






















                              up vote
                              -1
                              down vote













                              To check if date is today, use Android utils library




                              DateUtils.isToday(long timeInMilliseconds)




                              This utils class also offers human readable strings for relative times. For example,




                              DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42
                              minutes ago"




                              The are several parameters you can play with to define how precise the time span should be






                              share|improve this answer

















                              • 2




                                Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
                                – Calak
                                Nov 20 at 10:34















                              up vote
                              -1
                              down vote













                              To check if date is today, use Android utils library




                              DateUtils.isToday(long timeInMilliseconds)




                              This utils class also offers human readable strings for relative times. For example,




                              DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42
                              minutes ago"




                              The are several parameters you can play with to define how precise the time span should be






                              share|improve this answer

















                              • 2




                                Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
                                – Calak
                                Nov 20 at 10:34













                              up vote
                              -1
                              down vote










                              up vote
                              -1
                              down vote









                              To check if date is today, use Android utils library




                              DateUtils.isToday(long timeInMilliseconds)




                              This utils class also offers human readable strings for relative times. For example,




                              DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42
                              minutes ago"




                              The are several parameters you can play with to define how precise the time span should be






                              share|improve this answer












                              To check if date is today, use Android utils library




                              DateUtils.isToday(long timeInMilliseconds)




                              This utils class also offers human readable strings for relative times. For example,




                              DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42
                              minutes ago"




                              The are several parameters you can play with to define how precise the time span should be







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 20 at 10:16









                              Abhay Pratap

                              1




                              1








                              • 2




                                Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
                                – Calak
                                Nov 20 at 10:34














                              • 2




                                Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
                                – Calak
                                Nov 20 at 10:34








                              2




                              2




                              Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
                              – Calak
                              Nov 20 at 10:34




                              Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date)
                              – Calak
                              Nov 20 at 10:34


















                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f83244%2fchecking-whether-a-timestamp-is-10-minutes-old%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

                              Сан-Квентин

                              Алькесар

                              Josef Freinademetz