๋‚ ์งœ ์œ ํ˜• ์†Œ๊ฐœ - 1

"์•ˆ๋…•ํ•˜์„ธ์š”, ์•„๋ฏธ๊ณ . Date๋ผ๋Š” ํฅ๋ฏธ๋กœ์šด ์œ ํ˜•์— ๋Œ€ํ•ด ๋ง์”€๋“œ๋ฆฌ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์ด ์œ ํ˜•์€ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์„ ์ €์žฅํ•˜๊ณ  ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ๋„ ์ธก์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."

"ํฅ๋ฏธ๋กญ๊ฒŒ ๋“ค๋ฆฌ๋„ค์š”. ๊ณ„์†ํ•˜์„ธ์š”."

"๋ชจ๋“  Date ๊ฐ์ฒด๋Š” 1970๋…„ 1์›” 1์ผ GMT ์ดํ›„์˜ ์‹œ๊ฐ„์„ ๋‹ค์†Œ ํฅ๋ฏธ๋กœ์šด ํ˜•์‹์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค."

"์›Œ!"

"์˜ˆ. ์ด ์ˆซ์ž๋Š” ๋„ˆ๋ฌด ์ปค์„œ int ์— ์ €์žฅํ•  ๊ณต๊ฐ„์ด ๋ถ€์กฑํ•ด์„œ long ์— ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค . ํ•˜์ง€๋งŒ ๋‘ ๋‚ ์งœ ์‚ฌ์ด์˜ ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฐ ์ •๋ง ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„์˜ ์ •ํ™•๋„๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‚ ์งœ ๋ณ€๊ฒฝ์„ ๊ณผ ์ผ๊ด‘ ์ ˆ์•ฝ ์‹œ๊ฐ„์ œ ๋ฌธ์ œ๋„ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค."

"๊ฐ€์žฅ ํฅ๋ฏธ๋กœ์šด ๋ถ€๋ถ„์€ ๊ฐ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ ์‹œ ํ˜„์žฌ ์‹œ๊ฐ„์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ˜„์žฌ ์‹œ๊ฐ„์„ ์•Œ๋ ค๋ฉด 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()๋ฉ”์„œ๋“œ๋Š” Date ๊ฐœ์ฒด์— ์ €์žฅ๋œ ๋ฐ€๋ฆฌ์ดˆ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค."

" after()๋ฉ”์†Œ๋“œ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๋‚ ์งœ๊ฐ€ ๋ฉ”์„œ๋“œ์— ์ „๋‹ฌ๋œ ๋‚ ์งœ ์ดํ›„ ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค."

" getHours(), getMinutes(), getSeconds()๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœ๋œ ๊ฐœ์ฒด์— ๋Œ€ํ•ด ๊ฐ๊ฐ ์‹œ๊ฐ„, ๋ถ„, ์ดˆ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค."

"๋˜ํ•œ ๋งˆ์ง€๋ง‰ ์˜ˆ์—์„œ Date ๊ฐ์ฒด ์— ์ €์žฅ๋œ ๋‚ ์งœ/์‹œ๊ฐ„์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค . ํ˜„์žฌ ์‹œ๊ฐ„๊ณผ ๋‚ ์งœ๋ฅผ ๊ฐ€์ ธ์˜จ ๋‹ค์Œ ์‹œ, ๋ถ„, ์ดˆ๋ฅผ 0์œผ๋กœ ์žฌ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ 1์›”์„ ์›”๊ณผ 1์€ ์ผ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ yearStartTime๊ฐ์ฒด๋Š” ํ˜„์žฌ ์—ฐ๋„์˜ 1์›” 1์ผ ๋‚ ์งœ์™€ ์‹œ๊ฐ„ 00:00:00์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค."

"๊ทธ ํ›„ ๋‹ค์‹œ ํ˜„์žฌ ๋‚ ์งœ( )๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ  currentTime๋‘ ๋‚ ์งœ ๊ฐ„์˜ ์ฐจ์ด๋ฅผ ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„๋กœ ๊ณ„์‚ฐํ•˜์—ฌ ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค msTimeDifference."

"๊ทธ๋Ÿฐ ๋‹ค์Œ msTimeDifference24์‹œ๊ฐ„์˜ ๋ฐ€๋ฆฌ์ดˆ ์ˆ˜๋กœ ๋‚˜๋ˆ„์–ด ํ˜„์žฌ ์—ฐ๋„ ์‹œ์ž‘๋ถ€ํ„ฐ ์˜ค๋Š˜๊นŒ์ง€์˜ ์ „์ฒด ๋‚ ์งœ ์ˆ˜๋ฅผ ์–ป์Šต๋‹ˆ๋‹ค."

"์™€์šฐ! ์ •๋ง ๋ฉ‹์ง€๋‹ค!"