แนะนำประเภทวันที่ - 1

"สวัสดี Amigo ฉันอยากจะบอกคุณเกี่ยวกับประเภทที่น่าสนใจที่เรียกว่า Date ประเภทนี้จะเก็บวันที่และเวลาและยังสามารถวัดช่วงเวลาได้อีกด้วย"

"ฟังดูน่าสนใจ ไปต่อเถอะ"

"อ็อบเจกต์ Date ทุกอันจัดเก็บเวลาในรูปแบบที่ค่อนข้างน่าสนใจ: จำนวนมิลลิวินาทีตั้งแต่ 1 มกราคม 1970, GMT"

"โว้ว!"

"ใช่ ตัวเลขนี้ใหญ่มากจนไม่มีที่ว่างเพียงพอใน int ดังนั้นจึงต้องเก็บไว้เป็นเวลานานแต่มันมีประโยชน์มากในการคำนวณความแตกต่างระหว่างวันที่สองวันใดๆ คุณเพียงแค่ทำการลบเพื่อหาความแตกต่างด้วย แม่นยำระดับมิลลิวินาทีนอกจากนี้ ยังแก้ปัญหาเส้นวันที่และเวลาออมแสงได้อีกด้วย"

"ส่วนที่น่าสนใจที่สุดคือแต่ละออบเจกต์จะเริ่มต้นด้วยเวลาปัจจุบันเมื่อสร้าง หากต้องการทราบเวลาปัจจุบัน คุณเพียงแค่ต้องสร้างออบเจกต์ Date"

"คุณทำงานกับมันได้อย่างไร"

"นี่คือตัวอย่างบางส่วน:"

รับวันที่ปัจจุบัน:
public static void main(String[] args) throws Exception
{
     Date today = new Date();
     System.out.println("Current date: " + today);
}
คำนวณความแตกต่างระหว่างสองวัน
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()วิธีการส่งคืนจำนวนมิลลิวินาทีที่เก็บไว้ในวัตถุวันที่"

" after()เมธอดจะตรวจสอบว่าวันที่ที่เราเรียกใช้เมธอดนั้นมาหลังจากวันที่ผ่านไปยังเมธอดหรือไม่"

" getHours(), getMinutes(), getSeconds()เมธอดส่งคืนจำนวนชั่วโมง นาที และวินาที ตามลำดับ สำหรับวัตถุที่ถูกเรียกใช้"

"นอกจากนี้ ในตัวอย่างสุดท้าย คุณจะเห็นว่าคุณสามารถเปลี่ยนวันที่/เวลาที่จัดเก็บไว้ในวัตถุวันที่ เราได้รับเวลาและวันที่ปัจจุบัน จากนั้นรีเซ็ตชั่วโมง นาที และวินาทีเป็น 0 นอกจากนี้ เรายังตั้งค่าเดือนมกราคมเป็น เดือนและ 1 เป็นวันของเดือน ดังนั้นyearStartTimeออบเจกต์จึงเก็บวันที่ 1 มกราคมของปีปัจจุบันและเวลา 00:00:00 น."

"หลังจากนั้น เราจะได้วันที่ปัจจุบันอีกครั้ง ( currentTime) คำนวณความแตกต่างระหว่างสองวันในหน่วยมิลลิวินาที และเก็บไว้ในmsTimeDifference"

"จากนั้นเราหารmsTimeDifferenceด้วยจำนวนมิลลิวินาทีใน 24 ชั่วโมงเพื่อให้ได้จำนวนวันเต็มตั้งแต่ต้นปีจนถึงวันนี้"

"ว้าว! นี่มันเจ๋งมาก!"