"வணக்கம், அமிகோ. தேதி எனப்படும் ஒரு சுவாரஸ்யமான வகையைப் பற்றி நான் உங்களுக்குச் சொல்ல விரும்புகிறேன். இந்த வகை தேதி மற்றும் நேரத்தைச் சேமிக்கிறது, மேலும் நேர இடைவெளிகளையும் அளவிட முடியும்."
"சுவாரஸ்யமாக இருக்கிறது. தயவுசெய்து தொடரவும்."
"ஒவ்வொரு தேதிப் பொருளும் ஒரு நேரத்தை மிகவும் சுவாரஸ்யமான வடிவத்தில் சேமிக்கிறது: ஜனவரி 1, 1970, GMT முதல் மில்லி விநாடிகளின் எண்ணிக்கை."
"ஐயோ!"
"ஆமாம். இந்த எண் மிகவும் பெரியது, அதற்கு ஒரு முழு எண்ணாக இடமில்லை , எனவே அதை நீண்ட நேரம் சேமிக்க வேண்டும் . ஆனால் எந்த இரண்டு தேதிகளுக்கும் இடையே உள்ள வித்தியாசத்தைக் கணக்கிடுவதற்கு இது மிகவும் எளிது. வித்தியாசத்தைக் கண்டறிய நீங்கள் கழித்தல் செய்யுங்கள். மில்லி விநாடிக்கான துல்லியம். இது தேதிக் கோடு மற்றும் பகல் சேமிப்பு நேரத்தின் சிக்கலையும் தீர்க்கிறது."
"மிகவும் சுவாரஸ்யமான பகுதி என்னவென்றால், ஒவ்வொரு பொருளும் அதன் உருவாக்கத்தின் தற்போதைய நேரத்துடன் துவக்கப்படும். தற்போதைய நேரத்தை அறிய, நீங்கள் ஒரு தேதி பொருளை உருவாக்க வேண்டும்."
"நீங்கள் அதை எப்படி வேலை செய்கிறீர்கள்?"
"இங்கே சில உதாரணங்கள்:"
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 மணிநேரத்தில் உள்ள மில்லி விநாடிகளின் எண்ணிக்கையால் வகுப்போம் ."
"ஆஹா! இது மிகவும் அருமை!"
GO TO FULL VERSION