Come posso confrontare se mytime
è tra fromtime
e totime
:Confrontare due timestamp in java
Timestamp fromtime;
Timestamp totime;
Timestamp mytime;
Come posso confrontare se mytime
è tra fromtime
e totime
:Confrontare due timestamp in java
Timestamp fromtime;
Timestamp totime;
Timestamp mytime;
if(mytime.after(fromtime) && mytime.before(totime))
//mytime is in between
Utilizzare i before
e after
metodi: Javadoc
if (mytime.after(fromtime) && mytime.before(totime))
Da: http://download.oracle.com/javase/6/docs/api/java/sql/Timestamp.html#compareTo(java.sql.Timestamp)
public int compareTo(Timestamp ts)
paragona questo oggetto Timestamp al dato oggetto Timestamp. Parametri: ts - l'oggetto Timestamp da confrontare con questo oggetto Timestamp Restituisce: il valore 0 se i due oggetti Timestamp sono uguali; un valore inferiore a 0 se questo oggetto Timestamp è precedente all'argomento dato; e un valore maggiore di 0 se questo oggetto Timestamp è dopo l'argomento specificato. dal: 1,4
if (!mytime.before(fromtime) && !mytime.after(totime))
java.util.Date mytime = null;
if (mytime.after(now) && mytime.before(last_download_time))
ha funzionato per me
hmm .. a) che getterà un NPE b) vero solo per il download futuro, un timewarp ;-) – kleopatra
È possibile ordinare Timestamp come segue:
public int compare(Timestamp t1, Timestamp t2) {
long l1 = t1.getTime();
long l2 = t2.getTime();
if (l2 > l1)
return 1;
else if (l1 > l2)
return -1;
else
return 0;
}
solo convertire il timestamp nella rappresentazione millisecondi. Utilizzare il metodo getTime().
Tutte queste soluzioni non funzionano per me, anche se il modo giusto di pensare.
le seguenti opere per me:
if(mytime.isAfter(fromtime) || mytime.isBefore(totime)
// mytime is between fromtime and totime
Prima ho provato ho pensato la vostra soluzione con & & troppo
consideri risposta Maurice Perry, se si desidera includere '' fromTime' e toTime' –