Comparar uma data (RESOLVIDO)

3 respostas
O21

Como faço para compara se uma data é maior ou menor que outra data???

EX:

Date data1 = new Date();
Date data2 = getDate();

if(data1 &gt data2){

}

Onde getDate é retona um atributo Date que passo por um input…

:?: :arrow: :idea:

3 Respostas

kaabah

use o método compareTo()

compareTo

public int compareTo(Date anotherDate)

Compares two Dates for ordering.

Specified by:
    compareTo in interface Comparable<Date>

Parameters:
    anotherDate - the Date to be compared. 
Returns:
    the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument. 
Throws:
    NullPointerException - if anotherDate is null.
Since:
    1.2

:idea:

lina

Oi,

segue um exemplo de compareTo:

String
data_maior		=	"2004/03/01";
		
String
data_menor		=	"2003/03/01";
		
int
teste			=	data_maior.compareTo(data_menor);
		
System.out.println("Data maior "+teste);

Tchauzin!

O21

Grato a todos pela resposta. :thumbup:

Criado 4 de maio de 2007
Ultima resposta 4 de mai. de 2007
Respostas 3
Participantes 3