Nanosを含めてください。含めないと、些細なことです。
long diff = Math.abs(t1.getTime () - t2.getTime ());
[編集]最も正確な結果が必要なので、ダブルはありません。整数/長演算のみ。また、結果はポジティブでなければなりません。擬似コード:
Timestamp result = abs (t1 - t2);
例:
t1 = (time=1001, nanos=1000000), t2 = (time=999, nanos=999000000)
-> diff = (time=2, nanos=2000000)
はい、Java.sql.Timestampのミリ秒は時間とナノ秒パーで重複しているため、1001ミリ秒は1秒(1000)と1ミリ秒を意味します。これはtime
部分とnanos
にあります。 1ミリ秒= 1000000ナノ秒であるためです)。これは見た目よりもはるかに悪質です。
実際にコードをテストしたり、動作するコードサンプルを用意したりせずに、回答を投稿しないことをお勧めします:)
1時間とさまざまな単体テストを行った後、私はこのソリューションを思いつきました。
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 ());
どの単位で?上記の差分はミリ秒を示し、Timestamp.nanos()はミリ秒の(百万分の1?)単位のintを返します。
(t1.getTime () + (.000001*t1.getNanos()) - (t2.getTime () + (.000001*t2.getNanos())
または私は何かが欠けていますか?もう1つの質問は、このレベルの精度が必要ですか? JVMがこのレベルで正確であるとは保証されていません。データソースがそれほど正確であることが確実でない限り、問題ではないと思います。
Mmyersコードに基づいて構築しています...
import Java.math.BigInteger;
import Java.sql.Timestamp;
public class Main
{
// 1s == 1000ms == 1,000,000us == 1,000,000,000ns (1 billion ns)
public final static BigInteger ONE_BILLION = new BigInteger ("1000000000");
public static void main(String[] args) throws InterruptedException
{
final Timestamp t1;
final Timestamp t2;
final BigInteger firstTime;
final BigInteger secondTime;
final BigInteger diffTime;
t1 = new Timestamp(System.currentTimeMillis());
Thread.sleep(20);
t2 = new Timestamp(System.currentTimeMillis());
System.out.println(t1);
System.out.println(t2);
firstTime = BigInteger.valueOf(t1.getTime() / 1000 * 1000).multiply(ONE_BILLION ).add(BigInteger.valueOf(t1.getNanos()));
secondTime = BigInteger.valueOf(t2.getTime() / 1000 * 1000).multiply(ONE_BILLION ).add(BigInteger.valueOf(t2.getNanos()));
diffTime = firstTime.subtract(secondTime);
System.out.println(firstTime);
System.out.println(secondTime);
System.out.println(diffTime);
}
}
このメソッドを使用して、2 Java.sql.Timestmap
の差を取得します
/**
* Get a diff between two timestamps.
*
* @param oldTs The older timestamp
* @param newTs The newer timestamp
* @param timeUnit The unit in which you want the diff
* @return The diff value, in the provided time unit.
*/
public static long getDateDiff(Timestamp oldTs, Timestamp newTs, TimeUnit timeUnit) {
long diffInMS = newTs.getTime() - oldTs.getTime();
return timeUnit.convert(diffInMS, TimeUnit.MILLISECONDS);
}
// Examples:
// long diffMinutes = getDateDiff(oldTs, newTs, TimeUnit.MINUTES);
// long diffHours = getDateDiff(oldTs, newTs, TimeUnit.HOURS);
(回答を短くするために古いコードが削除されました)
編集2:新しいコード:
public class ArraySizeTest {
public static void main(String[] args) throws InterruptedException {
Timestamp t1 = new Timestamp(System.currentTimeMillis());
t1.setNanos(t1.getNanos() + 60);
Thread.sleep(20);
Timestamp t2 = new Timestamp(System.currentTimeMillis());
t2.setNanos(t2.getNanos() + 30);
System.out.println(t1);
System.out.println(t2);
// The actual diff...
long firstTime = (getTimeNoMillis(t1) * 1000000) + t1.getNanos();
long secondTime = (getTimeNoMillis(t2) * 1000000) + t2.getNanos();
long diff = Math.abs(firstTime - secondTime); // diff is in nanos
System.out.println(diff);
System.out.println(Math.abs(t1.getTime() - t2.getTime()));
}
private static long getTimeNoMillis(Timestamp t) {
return t.getTime() - (t.getNanos()/1000000);
}
}
出力:
2009-02-24 10:35:15.56500006 2009-02-24 10:35:15.59600003 30999970 31
編集3:タイムスタンプを返すものが必要な場合は、次を使用します。
public static Timestamp diff(Timestamp t1, Timestamp t2) {
long firstTime = (getTimeNoMillis(t1) * 1000000) + t1.getNanos();
long secondTime = (getTimeNoMillis(t2) * 1000000) + t2.getNanos();
long diff = Math.abs(firstTime - secondTime); // diff is in nanoseconds
Timestamp ret = new Timestamp(diff / 1000000);
ret.setNanos((int) (diff % 1000000000));
return ret;
}
private static long getTimeNoMillis(Timestamp t) {
return t.getTime() - (t.getNanos()/1000000);
}
このコードはユニットテストに合格します。