
"హాయ్, అమిగో. నేను మీకు తేదీ అనే ఆసక్తికరమైన రకం గురించి చెప్పాలనుకుంటున్నాను. ఈ రకం తేదీ మరియు సమయాన్ని నిల్వ చేస్తుంది మరియు సమయ విరామాలను కూడా కొలవగలదు."
"ఆసక్తికరంగా ఉంది. దయచేసి కొనసాగండి."
"ప్రతి తేదీ వస్తువు సమయాన్ని ఆసక్తికరమైన రూపంలో నిల్వ చేస్తుంది: జనవరి 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