日付型の紹介 - 1

「こんにちは、アミーゴ。Date と呼ばれる興味深い型についてお話したいと思います。この型は日付と時刻を保存し、時間間隔を測定することもできます。」

「面白そうですね。続けてください。」

「すべての Date オブジェクトは、GMT の 1970 年 1 月 1 日からのミリ秒数という、かなり興味深い形式で時刻を格納します。」

"うわあ!"

「そうです。この数値は大きすぎて、 intに格納する十分なスペースがないため、 longに格納する必要があります。しかし、これは、任意の 2 つの日付の差を計算するのに非常に便利です。差を見つけるには、引き算を行うだけです。ミリ秒単位の精度です。日付変更線と夏時間の問題も解決します。」

「最も興味深いのは、各オブジェクトが作成時に現在時刻で初期化されることです。現在時刻を知るには、Date オブジェクトを作成するだけです。」

「どうやってそれを扱うのですか?」

"ここではいくつかの例を示します:"

現在の日付を取得します。
public static void main(String[] args) throws Exception
{
     Date today = new Date();
     System.out.println("Current date: " + today);
}
2 つの日付の差を計算します
public static void main(String[] args) throws Exception
{
    Date currentTime = new Date();           // Get the current date and time
    Thread.sleep(3000);                      // Wait 3 seconds (3000 milliseconds)
    Date newTime = new Date();               // Get the new current time

    long msDelay = newTime.getTime() - currentTime.getTime(); // Calculate the difference
    System.out.println("Time difference is: " + msDelay + " in ms");
}
一定の時間が経過したかどうかを確認します。
public static void main(String[] args) throws Exception
{
    Date startTime = new Date();

    long endTime = startTime.getTime() + 5000;  //    +5 seconds
    Date endDate = new Date(endTime);

    Thread.sleep(3000);              // Wait 3 seconds

    Date currentTime = new Date();
    if(currentTime.after(endDate))// Check whether currentTime is after endDate
    {
        System.out.println("End time!");
    }
}
一日の始まりからどれくらいの時間が経過したかを確認します。
public static void main(String[] args) throws Exception
{
    Date currentTime = new Date();
    int hours = currentTime.getHours();
    int mins = currentTime.getMinutes();
    int secs = currentTime.getSeconds();

    System.out.println("Time since midnight " + hours + ":" + mins + ":" + secs);
}
年の初めから何日が経過したかを確認します。
public static void main(String[] args) throws Exception
{
    Date yearStartTime = new Date();
    yearStartTime.setHours(0);
    yearStartTime.setMinutes(0);
    yearStartTime.setSeconds(0);

    yearStartTime.setDate(1);      // First day of the month
    yearStartTime.setMonth(0);     // January (the months are indexed from 0 to 11)

    Date currentTime = new Date();
    long msTimeDifference = currentTime.getTime() - yearStartTime.getTime();
    long msDay = 24 * 60 * 60 * 1000;  // The number of milliseconds in 24 hours

    int dayCount = (int) (msTimeDifference/msDay); // The number of full days
    System.out.println("Days since the start of the year: " + dayCount);
}

「このgetTime()メソッドは、Date オブジェクトに格納されているミリ秒数を返します。」

after()メソッドは、メソッドを呼び出した日付が、メソッドに渡された日付より後であるかどうかを確認します。」

getHours(), getMinutes(), getSeconds()メソッドは、呼び出されたオブジェクトの時間、分、秒をそれぞれ返します。」

さらに、最後の例では、 Dateオブジェクトに格納されている日付/時刻を変更できることがわかります。現在の時刻と日付を取得し、時、分、秒を 0 にリセットします。また、1 月をmonth と 1 が月の日として格納されます。したがって、yearStartTimeオブジェクトには今年の 1 月 1 日の日付と時刻 00:00:00 が格納されます。

「その後、再び現在の日付 ( currentTime) を取得し、2 つの日付の差をミリ秒単位で計算し、それを に保存しますmsTimeDifference。」

msTimeDifference「次に、24 時間のミリ秒数で割って、今年の初めから今日までの丸日数を取得します。」

「うわー!これはすごい!」