<legend id='IJeM9'><style id='IJeM9'><dir id='IJeM9'><q id='IJeM9'></q></dir></style></legend>

    • <bdo id='IJeM9'></bdo><ul id='IJeM9'></ul>
    <tfoot id='IJeM9'></tfoot>

        <small id='IJeM9'></small><noframes id='IJeM9'>

      1. <i id='IJeM9'><tr id='IJeM9'><dt id='IJeM9'><q id='IJeM9'><span id='IJeM9'><b id='IJeM9'><form id='IJeM9'><ins id='IJeM9'></ins><ul id='IJeM9'></ul><sub id='IJeM9'></sub></form><legend id='IJeM9'></legend><bdo id='IJeM9'><pre id='IJeM9'><center id='IJeM9'></center></pre></bdo></b><th id='IJeM9'></th></span></q></dt></tr></i><div id='IJeM9'><tfoot id='IJeM9'></tfoot><dl id='IJeM9'><fieldset id='IJeM9'></fieldset></dl></div>

      2. 如何计算两个 Java java.sql.Timestamps 之间的差异?

        时间:2023-07-25
          <tbody id='ZSEww'></tbody>
          <bdo id='ZSEww'></bdo><ul id='ZSEww'></ul>

              <i id='ZSEww'><tr id='ZSEww'><dt id='ZSEww'><q id='ZSEww'><span id='ZSEww'><b id='ZSEww'><form id='ZSEww'><ins id='ZSEww'></ins><ul id='ZSEww'></ul><sub id='ZSEww'></sub></form><legend id='ZSEww'></legend><bdo id='ZSEww'><pre id='ZSEww'><center id='ZSEww'></center></pre></bdo></b><th id='ZSEww'></th></span></q></dt></tr></i><div id='ZSEww'><tfoot id='ZSEww'></tfoot><dl id='ZSEww'><fieldset id='ZSEww'></fieldset></dl></div>

                  <legend id='ZSEww'><style id='ZSEww'><dir id='ZSEww'><q id='ZSEww'></q></dir></style></legend>

                  <small id='ZSEww'></small><noframes id='ZSEww'>

                • <tfoot id='ZSEww'></tfoot>
                  本文介绍了如何计算两个 Java java.sql.Timestamps 之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  请包括纳米,否则将是微不足道的:

                  Please include the nanos, otherwise it would be trivial:

                  long diff = Math.abs(t1.getTime () - t2.getTime ());
                  

                  我想要最精确的结果,所以没有双打;只有整数/长算法.此外,结果必须是积极的.伪代码:

                  I want the most precise result, so no doubles; only integer/long arithmetic. Also, the result must be positive. Pseudo code:

                  Timestamp result = abs (t1 - t2);
                  

                  例子:

                  t1 = (time=1001, nanos=1000000), t2 = (time=999, nanos=999000000)
                   -> diff = (time=2, nanos=2000000)
                  

                  是的,java.sql.Timestamp 中的毫秒在 time 和 nanos par 中是重复的,所以 1001 毫秒意味着 1 秒 (1000) 和 1 毫秒,它在 time 部分和 nanos 部分,因为 1 毫秒 = 1000000 纳秒).这比看起来要狡猾得多.

                  Yes, milliseconds in java.sql.Timestamp are duplicated in the time and the nanos par, so 1001 milliseconds means 1 second (1000) and 1 milli which is in the time part and the nanos part because 1 millisecond = 1000000 nanoseconds). This is much more devious than it looks.

                  我建议不要在没有实际测试代码或准备好工作代码示例的情况下发布答案:)

                  I suggest not to post an answer without actually testing the code or having a working code sample ready :)

                  推荐答案

                  经过一个小时和各种单元测试,我想出了这个解决方案:

                  After one hour and various unit tests, I came up with this solution:

                  public static Timestamp diff (java.util.Date t1, java.util.Date t2)
                  {
                      // Make sure the result is always > 0
                      if (t1.compareTo (t2) < 0)
                      {
                          java.util.Date tmp = t1;
                          t1 = t2;
                          t2 = tmp;
                      }
                  
                      // Timestamps mix milli and nanoseconds in the API, so we have to separate the two
                      long diffSeconds = (t1.getTime () / 1000) - (t2.getTime () / 1000);
                      // For normals dates, we have millisecond precision
                      int nano1 = ((int) t1.getTime () % 1000) * 1000000;
                      // If the parameter is a Timestamp, we have additional precision in nanoseconds
                      if (t1 instanceof Timestamp)
                          nano1 = ((Timestamp)t1).getNanos ();
                      int nano2 = ((int) t2.getTime () % 1000) * 1000000;
                      if (t2 instanceof Timestamp)
                          nano2 = ((Timestamp)t2).getNanos ();
                  
                      int diffNanos = nano1 - nano2;
                      if (diffNanos < 0)
                      {
                          // Borrow one second
                          diffSeconds --;
                          diffNanos += 1000000000;
                      }
                  
                      // mix nanos and millis again
                      Timestamp result = new Timestamp ((diffSeconds * 1000) + (diffNanos / 1000000));
                      // setNanos() with a value of in the millisecond range doesn't affect the value of the time field
                      // while milliseconds in the time field will modify nanos! Damn, this API is a *mess*
                      result.setNanos (diffNanos);
                      return result;
                  }
                  

                  单元测试:

                      Timestamp t1 = new Timestamp (0);
                      Timestamp t3 = new Timestamp (999);
                      Timestamp t4 = new Timestamp (5001);
                      // Careful here; internally, Java has set nanos already!
                      t4.setNanos (t4.getNanos () + 1);
                  
                      // Show what a mess this API is...
                      // Yes, the milliseconds show up in *both* fields! Isn't that fun?
                      assertEquals (999, t3.getTime ());
                      assertEquals (999000000, t3.getNanos ());
                      // This looks weird but t4 contains 5 seconds, 1 milli, 1 nano.
                      // The lone milli is in both results ...
                      assertEquals (5001, t4.getTime ());
                      assertEquals (1000001, t4.getNanos ());
                  
                      diff = DBUtil.diff (t1, t4);
                      assertEquals (5001, diff.getTime ());
                      assertEquals (1000001, diff.getNanos ());
                  
                      diff = DBUtil.diff (t4, t3);
                      assertEquals (4002, diff.getTime ());
                      assertEquals (2000001, diff.getNanos ());
                  

                  这篇关于如何计算两个 Java java.sql.Timestamps 之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:来自 ZonedDateTime UTC 实例的 Java 日期和时间戳 下一篇:如何将 Timestamp 转换为 Date 或 DateTime 对象?

                  相关文章

                  最新文章

                  <tfoot id='gdCR6'></tfoot>
                  • <bdo id='gdCR6'></bdo><ul id='gdCR6'></ul>

                  <small id='gdCR6'></small><noframes id='gdCR6'>

                • <i id='gdCR6'><tr id='gdCR6'><dt id='gdCR6'><q id='gdCR6'><span id='gdCR6'><b id='gdCR6'><form id='gdCR6'><ins id='gdCR6'></ins><ul id='gdCR6'></ul><sub id='gdCR6'></sub></form><legend id='gdCR6'></legend><bdo id='gdCR6'><pre id='gdCR6'><center id='gdCR6'></center></pre></bdo></b><th id='gdCR6'></th></span></q></dt></tr></i><div id='gdCR6'><tfoot id='gdCR6'></tfoot><dl id='gdCR6'><fieldset id='gdCR6'></fieldset></dl></div>

                      <legend id='gdCR6'><style id='gdCR6'><dir id='gdCR6'><q id='gdCR6'></q></dir></style></legend>