2011-10-27 13 views

risposta

49
if(mytime.after(fromtime) && mytime.before(totime)) 
    //mytime is in between 
+0

consideri risposta Maurice Perry, se si desidera includere '' fromTime' e toTime' –

12

Utilizzare i before e after metodi: Javadoc

if (mytime.after(fromtime) && mytime.before(totime)) 
7

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

4
if (!mytime.before(fromtime) && !mytime.after(totime)) 
1
java.util.Date mytime = null; 
if (mytime.after(now) && mytime.before(last_download_time)) 

ha funzionato per me

+1

hmm .. a) che getterà un NPE b) vero solo per il download futuro, un timewarp ;-) – kleopatra

1

È 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; 
} 
-2

solo convertire il timestamp nella rappresentazione millisecondi. Utilizzare il metodo getTime().

0

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