Getting the current timestamp and compute the timestamp difference in minutesDetermining how long ago a page was last editedDate Format providerHave I coded this small object grouping script Pythonically?Get the difference between two dates, in the most convenient unitConverting UTC time to date in d3Simple elapsed time counterPulling time and date from the internetConvert FoxPro/dBase DATETIME field to Win32 SYSTEMTIME structGetting current timezoneA portable cross platform C++17 method to retrieve the current date and time

If I can solve Sudoku can I solve Travelling Salesman Problem(TSP)? If yes, how?

What is the significance behind "40 days" that often appears in the Bible?

Did Ender ever learn that he killed Stilson and/or Bonzo?

Sailing the cryptic seas

Life insurance that covers only simultaneous/dual deaths

What has been your most complicated TikZ drawing?

Happy pi day, everyone!

What do Xenomorphs eat in the Alien series?

In a future war, an old lady is trying to raise a boy but one of the weapons has made everyone deaf

Time travel from stationary position?

How to terminate ping <dest> &

How to deal with taxi scam when on vacation?

How do I hide Chekhov's Gun?

How to create the Curved texte?

How to explain that I do not want to visit a country due to personal safety concern?

How can I track script which gives me "command not found" right after the login?

How to change two letters closest to a string and one letter immediately after a string using notepad++

My Graph Theory Students

PTIJ: Who should I vote for? (21st Knesset Edition)

How to simplify this time periods definition interface?

Does Wild Magic Surge trigger off of spells on the Sorcerer spell list, if I learned them from another class?

Identifying the interval from A♭ to D♯

Do I need life insurance if I can cover my own funeral costs?

Is a party consisting of only a bard, a cleric, and a warlock functional long-term?



Getting the current timestamp and compute the timestamp difference in minutes


Determining how long ago a page was last editedDate Format providerHave I coded this small object grouping script Pythonically?Get the difference between two dates, in the most convenient unitConverting UTC time to date in d3Simple elapsed time counterPulling time and date from the internetConvert FoxPro/dBase DATETIME field to Win32 SYSTEMTIME structGetting current timezoneA portable cross platform C++17 method to retrieve the current date and time













1












$begingroup$


I have this code to get the current Timestamp and compute the last timestamp's difference with the current timestamp in minutes. I'm wondering if this can be optimized further for production.



public static Timestamp getTimestamp() 
java.util.Date date= new java.util.Date();
long time = date.getTime();
java.sql.Timestamp ts = new java.sql.Timestamp(time);
return ts;


public static long getLastTimestampElapse(java.sql.Timestamp oldTime)
long milliseconds1 = oldTime.getTime();
long milliseconds2 = getTimestamp().getTime();
long diff = milliseconds2 - milliseconds1;
long diffMinutes = diff / (60 * 1000);
return diffMinutes;










share|improve this question











$endgroup$
















    1












    $begingroup$


    I have this code to get the current Timestamp and compute the last timestamp's difference with the current timestamp in minutes. I'm wondering if this can be optimized further for production.



    public static Timestamp getTimestamp() 
    java.util.Date date= new java.util.Date();
    long time = date.getTime();
    java.sql.Timestamp ts = new java.sql.Timestamp(time);
    return ts;


    public static long getLastTimestampElapse(java.sql.Timestamp oldTime)
    long milliseconds1 = oldTime.getTime();
    long milliseconds2 = getTimestamp().getTime();
    long diff = milliseconds2 - milliseconds1;
    long diffMinutes = diff / (60 * 1000);
    return diffMinutes;










    share|improve this question











    $endgroup$














      1












      1








      1





      $begingroup$


      I have this code to get the current Timestamp and compute the last timestamp's difference with the current timestamp in minutes. I'm wondering if this can be optimized further for production.



      public static Timestamp getTimestamp() 
      java.util.Date date= new java.util.Date();
      long time = date.getTime();
      java.sql.Timestamp ts = new java.sql.Timestamp(time);
      return ts;


      public static long getLastTimestampElapse(java.sql.Timestamp oldTime)
      long milliseconds1 = oldTime.getTime();
      long milliseconds2 = getTimestamp().getTime();
      long diff = milliseconds2 - milliseconds1;
      long diffMinutes = diff / (60 * 1000);
      return diffMinutes;










      share|improve this question











      $endgroup$




      I have this code to get the current Timestamp and compute the last timestamp's difference with the current timestamp in minutes. I'm wondering if this can be optimized further for production.



      public static Timestamp getTimestamp() 
      java.util.Date date= new java.util.Date();
      long time = date.getTime();
      java.sql.Timestamp ts = new java.sql.Timestamp(time);
      return ts;


      public static long getLastTimestampElapse(java.sql.Timestamp oldTime)
      long milliseconds1 = oldTime.getTime();
      long milliseconds2 = getTimestamp().getTime();
      long diff = milliseconds2 - milliseconds1;
      long diffMinutes = diff / (60 * 1000);
      return diffMinutes;







      java performance datetime






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago









      Jamal

      30.4k11121227




      30.4k11121227










      asked yesterday









      RanPaulRanPaul

      3902513




      3902513




















          1 Answer
          1






          active

          oldest

          votes


















          4












          $begingroup$

          Note that none of these suggestions will have any significant impact on the performance of your application overall. Don't micro-optimize performance until you have known, tested bottlenecks.



          The getTimestamp() method is noise. If all you care about is the current timestamp in milliseconds, use System.currentTimeMillis().



          You can use a constant to store the number of milliseconds in a minute, potentially saving the multiplication. Even if the compiler optimizes the math away, it's easier to read.



          A java.sql.Timestamp is a kind of java.util.Date, and the getTime() method is defined there. Your method should accept a java.util.Date to support more clients at no cost.



          Your method is poorly named. Something like getMinutesSince() would be more readable. Likewise, there are better variable names than what you've selected.



          Use final to indicate that variables won't be reassigned. That reduces the cognitive load on the reader.



          You don't really need as many variables as you have. You might even be able to get away with none and still have a reasonably clear method.



          If you were to use all my suggestions, your code might look more like:



          private static final long MILLISECONDS_PER_MINUTE = 60 * 1000;

          public static long getMinutesSince(final java.util.Date startTime)
          final long millisecondsSinceStart =
          System.currentTimeMillis() - startTime.getTime();
          return millisecondsSinceStart / MILLISECONDS_PER_MINUTE;






          share|improve this answer











          $endgroup$








          • 1




            $begingroup$
            You might want to assign MILLISECONDS_PER_MINUTE to something. :-)
            $endgroup$
            – AJNeufeld
            yesterday






          • 1




            $begingroup$
            TimeUnit.MILLISECONDS.toMinutes(millisecondsSinceStart) - TimeUnit already has constants for you
            $endgroup$
            – Alexey Ragozin
            yesterday










          • $begingroup$
            @AJNeufeld That's what I get for editing directly in the window instead of moving to my editor. Fixed, thanks!
            $endgroup$
            – Eric Stein
            16 hours ago










          • $begingroup$
            @AlexeyRagozin That's a good point. I personally find it harder to read in this case, but it's a good option for the OP to be aware of. Thanks!
            $endgroup$
            – Eric Stein
            16 hours ago










          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',
          autoActivateHeartbeat: false,
          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%2f215432%2fgetting-the-current-timestamp-and-compute-the-timestamp-difference-in-minutes%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









          4












          $begingroup$

          Note that none of these suggestions will have any significant impact on the performance of your application overall. Don't micro-optimize performance until you have known, tested bottlenecks.



          The getTimestamp() method is noise. If all you care about is the current timestamp in milliseconds, use System.currentTimeMillis().



          You can use a constant to store the number of milliseconds in a minute, potentially saving the multiplication. Even if the compiler optimizes the math away, it's easier to read.



          A java.sql.Timestamp is a kind of java.util.Date, and the getTime() method is defined there. Your method should accept a java.util.Date to support more clients at no cost.



          Your method is poorly named. Something like getMinutesSince() would be more readable. Likewise, there are better variable names than what you've selected.



          Use final to indicate that variables won't be reassigned. That reduces the cognitive load on the reader.



          You don't really need as many variables as you have. You might even be able to get away with none and still have a reasonably clear method.



          If you were to use all my suggestions, your code might look more like:



          private static final long MILLISECONDS_PER_MINUTE = 60 * 1000;

          public static long getMinutesSince(final java.util.Date startTime)
          final long millisecondsSinceStart =
          System.currentTimeMillis() - startTime.getTime();
          return millisecondsSinceStart / MILLISECONDS_PER_MINUTE;






          share|improve this answer











          $endgroup$








          • 1




            $begingroup$
            You might want to assign MILLISECONDS_PER_MINUTE to something. :-)
            $endgroup$
            – AJNeufeld
            yesterday






          • 1




            $begingroup$
            TimeUnit.MILLISECONDS.toMinutes(millisecondsSinceStart) - TimeUnit already has constants for you
            $endgroup$
            – Alexey Ragozin
            yesterday










          • $begingroup$
            @AJNeufeld That's what I get for editing directly in the window instead of moving to my editor. Fixed, thanks!
            $endgroup$
            – Eric Stein
            16 hours ago










          • $begingroup$
            @AlexeyRagozin That's a good point. I personally find it harder to read in this case, but it's a good option for the OP to be aware of. Thanks!
            $endgroup$
            – Eric Stein
            16 hours ago















          4












          $begingroup$

          Note that none of these suggestions will have any significant impact on the performance of your application overall. Don't micro-optimize performance until you have known, tested bottlenecks.



          The getTimestamp() method is noise. If all you care about is the current timestamp in milliseconds, use System.currentTimeMillis().



          You can use a constant to store the number of milliseconds in a minute, potentially saving the multiplication. Even if the compiler optimizes the math away, it's easier to read.



          A java.sql.Timestamp is a kind of java.util.Date, and the getTime() method is defined there. Your method should accept a java.util.Date to support more clients at no cost.



          Your method is poorly named. Something like getMinutesSince() would be more readable. Likewise, there are better variable names than what you've selected.



          Use final to indicate that variables won't be reassigned. That reduces the cognitive load on the reader.



          You don't really need as many variables as you have. You might even be able to get away with none and still have a reasonably clear method.



          If you were to use all my suggestions, your code might look more like:



          private static final long MILLISECONDS_PER_MINUTE = 60 * 1000;

          public static long getMinutesSince(final java.util.Date startTime)
          final long millisecondsSinceStart =
          System.currentTimeMillis() - startTime.getTime();
          return millisecondsSinceStart / MILLISECONDS_PER_MINUTE;






          share|improve this answer











          $endgroup$








          • 1




            $begingroup$
            You might want to assign MILLISECONDS_PER_MINUTE to something. :-)
            $endgroup$
            – AJNeufeld
            yesterday






          • 1




            $begingroup$
            TimeUnit.MILLISECONDS.toMinutes(millisecondsSinceStart) - TimeUnit already has constants for you
            $endgroup$
            – Alexey Ragozin
            yesterday










          • $begingroup$
            @AJNeufeld That's what I get for editing directly in the window instead of moving to my editor. Fixed, thanks!
            $endgroup$
            – Eric Stein
            16 hours ago










          • $begingroup$
            @AlexeyRagozin That's a good point. I personally find it harder to read in this case, but it's a good option for the OP to be aware of. Thanks!
            $endgroup$
            – Eric Stein
            16 hours ago













          4












          4








          4





          $begingroup$

          Note that none of these suggestions will have any significant impact on the performance of your application overall. Don't micro-optimize performance until you have known, tested bottlenecks.



          The getTimestamp() method is noise. If all you care about is the current timestamp in milliseconds, use System.currentTimeMillis().



          You can use a constant to store the number of milliseconds in a minute, potentially saving the multiplication. Even if the compiler optimizes the math away, it's easier to read.



          A java.sql.Timestamp is a kind of java.util.Date, and the getTime() method is defined there. Your method should accept a java.util.Date to support more clients at no cost.



          Your method is poorly named. Something like getMinutesSince() would be more readable. Likewise, there are better variable names than what you've selected.



          Use final to indicate that variables won't be reassigned. That reduces the cognitive load on the reader.



          You don't really need as many variables as you have. You might even be able to get away with none and still have a reasonably clear method.



          If you were to use all my suggestions, your code might look more like:



          private static final long MILLISECONDS_PER_MINUTE = 60 * 1000;

          public static long getMinutesSince(final java.util.Date startTime)
          final long millisecondsSinceStart =
          System.currentTimeMillis() - startTime.getTime();
          return millisecondsSinceStart / MILLISECONDS_PER_MINUTE;






          share|improve this answer











          $endgroup$



          Note that none of these suggestions will have any significant impact on the performance of your application overall. Don't micro-optimize performance until you have known, tested bottlenecks.



          The getTimestamp() method is noise. If all you care about is the current timestamp in milliseconds, use System.currentTimeMillis().



          You can use a constant to store the number of milliseconds in a minute, potentially saving the multiplication. Even if the compiler optimizes the math away, it's easier to read.



          A java.sql.Timestamp is a kind of java.util.Date, and the getTime() method is defined there. Your method should accept a java.util.Date to support more clients at no cost.



          Your method is poorly named. Something like getMinutesSince() would be more readable. Likewise, there are better variable names than what you've selected.



          Use final to indicate that variables won't be reassigned. That reduces the cognitive load on the reader.



          You don't really need as many variables as you have. You might even be able to get away with none and still have a reasonably clear method.



          If you were to use all my suggestions, your code might look more like:



          private static final long MILLISECONDS_PER_MINUTE = 60 * 1000;

          public static long getMinutesSince(final java.util.Date startTime)
          final long millisecondsSinceStart =
          System.currentTimeMillis() - startTime.getTime();
          return millisecondsSinceStart / MILLISECONDS_PER_MINUTE;







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 16 hours ago

























          answered yesterday









          Eric SteinEric Stein

          4,262613




          4,262613







          • 1




            $begingroup$
            You might want to assign MILLISECONDS_PER_MINUTE to something. :-)
            $endgroup$
            – AJNeufeld
            yesterday






          • 1




            $begingroup$
            TimeUnit.MILLISECONDS.toMinutes(millisecondsSinceStart) - TimeUnit already has constants for you
            $endgroup$
            – Alexey Ragozin
            yesterday










          • $begingroup$
            @AJNeufeld That's what I get for editing directly in the window instead of moving to my editor. Fixed, thanks!
            $endgroup$
            – Eric Stein
            16 hours ago










          • $begingroup$
            @AlexeyRagozin That's a good point. I personally find it harder to read in this case, but it's a good option for the OP to be aware of. Thanks!
            $endgroup$
            – Eric Stein
            16 hours ago












          • 1




            $begingroup$
            You might want to assign MILLISECONDS_PER_MINUTE to something. :-)
            $endgroup$
            – AJNeufeld
            yesterday






          • 1




            $begingroup$
            TimeUnit.MILLISECONDS.toMinutes(millisecondsSinceStart) - TimeUnit already has constants for you
            $endgroup$
            – Alexey Ragozin
            yesterday










          • $begingroup$
            @AJNeufeld That's what I get for editing directly in the window instead of moving to my editor. Fixed, thanks!
            $endgroup$
            – Eric Stein
            16 hours ago










          • $begingroup$
            @AlexeyRagozin That's a good point. I personally find it harder to read in this case, but it's a good option for the OP to be aware of. Thanks!
            $endgroup$
            – Eric Stein
            16 hours ago







          1




          1




          $begingroup$
          You might want to assign MILLISECONDS_PER_MINUTE to something. :-)
          $endgroup$
          – AJNeufeld
          yesterday




          $begingroup$
          You might want to assign MILLISECONDS_PER_MINUTE to something. :-)
          $endgroup$
          – AJNeufeld
          yesterday




          1




          1




          $begingroup$
          TimeUnit.MILLISECONDS.toMinutes(millisecondsSinceStart) - TimeUnit already has constants for you
          $endgroup$
          – Alexey Ragozin
          yesterday




          $begingroup$
          TimeUnit.MILLISECONDS.toMinutes(millisecondsSinceStart) - TimeUnit already has constants for you
          $endgroup$
          – Alexey Ragozin
          yesterday












          $begingroup$
          @AJNeufeld That's what I get for editing directly in the window instead of moving to my editor. Fixed, thanks!
          $endgroup$
          – Eric Stein
          16 hours ago




          $begingroup$
          @AJNeufeld That's what I get for editing directly in the window instead of moving to my editor. Fixed, thanks!
          $endgroup$
          – Eric Stein
          16 hours ago












          $begingroup$
          @AlexeyRagozin That's a good point. I personally find it harder to read in this case, but it's a good option for the OP to be aware of. Thanks!
          $endgroup$
          – Eric Stein
          16 hours ago




          $begingroup$
          @AlexeyRagozin That's a good point. I personally find it harder to read in this case, but it's a good option for the OP to be aware of. Thanks!
          $endgroup$
          – Eric Stein
          16 hours ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • 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.

          Use MathJax to format equations. MathJax reference.


          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%2fcodereview.stackexchange.com%2fquestions%2f215432%2fgetting-the-current-timestamp-and-compute-the-timestamp-difference-in-minutes%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

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

          Prove that NP is closed under karp reduction?Space(n) not closed under Karp reductions - what about NTime(n)?Class P is closed under rotation?Prove or disprove that $NL$ is closed under polynomial many-one reductions$mathbfNC_2$ is closed under log-space reductionOn Karp reductionwhen can I know if a class (complexity) is closed under reduction (cook/karp)Check if class $PSPACE$ is closed under polyonomially space reductionIs NPSPACE also closed under polynomial-time reduction and under log-space reduction?Prove PSPACE is closed under complement?Prove PSPACE is closed under union?

          Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar