Se si sta insieme sull'utilizzo di Java Date piuttosto che, diciamo, JodaTime, utilizzare un java.text.DateFormat
per convertire la stringa in una data, quindi confrontare i due utilizzando .equals:
Ho quasi dimenticato: è necessario azzerare le ore, i minuti, i secondi e i millisecondi della data corrente prima di confrontarli. Ho usato un oggetto Calendar
qui sotto per farlo.
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
// Other code here
String toDate;
//toDate = "05/11/2010";
// Value assigned to toDate somewhere in here
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
Calendar currDtCal = Calendar.getInstance();
// Zero out the hour, minute, second, and millisecond
currDtCal.set(Calendar.HOUR_OF_DAY, 0);
currDtCal.set(Calendar.MINUTE, 0);
currDtCal.set(Calendar.SECOND, 0);
currDtCal.set(Calendar.MILLISECOND, 0);
Date currDt = currDtCal.getTime();
Date toDt;
try {
toDt = df.parse(toDate);
} catch (ParseException e) {
toDt = null;
// Print some error message back to the user
}
if (currDt.equals(toDt)) {
// They're the same date
}
La tua domanda iniziale è stata onestamente detta terribile. L'ho migliorato In futuro cerca di essere il più chiaro possibile. Non ripetere le cose 3 volte e/o cose inutilmente in grassetto :) – BalusC
questo potrebbe aiutare se si considera l'uso della libreria joda http://stackoverflow.com/questions/1439779/how-to-compare-two-dates-without- the time-portion – ukanth
JodaTime offre molto più dell'API Date di Java ... http://joda-time.sourceforge.net/. Ciò renderebbe molto più semplice questo e gli altri compiti con le date. – harschware