"์๋ ํ์ธ์, ์๋ฏธ๊ณ . 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
."
"๊ทธ๋ฐ ๋ค์ msTimeDifference
24์๊ฐ์ ๋ฐ๋ฆฌ์ด ์๋ก ๋๋์ด ํ์ฌ ์ฐ๋ ์์๋ถํฐ ์ค๋๊น์ง์ ์ ์ฒด ๋ ์ง ์๋ฅผ ์ป์ต๋๋ค."
"์์ฐ! ์ ๋ง ๋ฉ์ง๋ค!"
GO TO FULL VERSION