"Ciao, Amigo. Voglio parlarti di un tipo interessante chiamato Data. Questo tipo memorizza la data e l'ora, e può anche misurare gli intervalli di tempo."
"Sembra interessante. Per favore, continua."
"Ogni oggetto Date memorizza un tempo in una forma piuttosto interessante: il numero di millisecondi dal 1° gennaio 1970, GMT."
"Ehi!"
"Sì. Questo numero è così grande che non c'è abbastanza spazio per esso in un int , quindi deve essere memorizzato in un long . Ma è davvero utile per calcolare la differenza tra due date qualsiasi. Basta fare la sottrazione per trovare la differenza con precisione al millisecondo. Risolve anche il problema della linea della data e dell'ora legale."
"La parte più interessante è che ogni oggetto viene inizializzato con l'ora corrente alla sua creazione. Per conoscere l'ora corrente, devi solo creare un oggetto Date."
"Come ci lavori?"
"Ecco alcuni esempi:"
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);
}
"Il getTime()
metodo restituisce il numero di millisecondi memorizzati in un oggetto Date."
"Il after()
metodo controlla se la data in cui abbiamo chiamato il metodo arriva dopo la data passata al metodo."
"I getHours(), getMinutes(), getSeconds()
metodi restituiscono rispettivamente il numero di ore, minuti e secondi per l'oggetto su cui sono stati chiamati."
"Inoltre, nell'ultimo esempio puoi vedere che puoi modificare la data/ora memorizzata in un oggetto Date . Otteniamo l'ora e la data correnti e quindi reimpostiamo le ore, i minuti e i secondi su 0. Impostiamo anche gennaio come mese e 1 come giorno del mese. Pertanto, l' yearStartTime
oggetto memorizza la data 1 gennaio dell'anno corrente e l'ora 00:00:00."
"Successivamente, otteniamo nuovamente la data corrente ( currentTime
), calcoliamo la differenza tra le due date in millisecondi e la memorizziamo in msTimeDifference
."
"Quindi dividiamo msTimeDifference
per il numero di millisecondi in 24 ore per ottenere il numero di giorni interi dall'inizio dell'anno in corso fino ad oggi."
"Wow! È fantastico!"
GO TO FULL VERSION