2時間の差があるとしましょう
java.text.DateFormat f = new java.text.SimpleDateFormat("HH:mm");
java.util.Date checkIn = f.parse("00:00");
java.util.Date checkOut = f.parse("05:00");
Long timeDifference = new Long(checkOut.getTime() - checkIn.getTime());
"timeDifference" を 3600000 (ミリ秒単位で 1 時間) で割ると、この間隔が何時間になるかがわかります。正しい結果は 5 です。
しかし、「timeDifference」を次のように変換しようとすると:
Calendar cal = new GregorianCalendar();
cal.setTime(new Date(timeDifference));
DateFormat formatter = new SimpleDateFormat("HH:mm");
formatter.format(cal.getTime());
「02:00」と表示されます...なぜですか?「timeDifference」をフォーマットするにはどうすればよいですか?
編集:日付はあまり気にしません。HH:mm のような時間形式で、checkIn と checkOut の違いを知りたいだけです。