Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare date without time [duplicate]

Tags:

java

time

Possible Duplicate:
How to compare two Dates without the time portion?

How to compare date without time in java ?

Date currentDate = new Date();// get current date           
Date eventDate = tempAppointments.get(i).mStartDate;
int dateMargin = currentDate.compareTo(eventDate); 

this code compares time and date !

like image 856
Adham Avatar asked Oct 16 '11 11:10

Adham


People also ask

How do you compare two dates without the time portion?

If you want to compare just the date part without considering time, you need to use DateFormat class to format the date into some format and then compare their String value. Alternatively, you can use joda-time which provides a class LocalDate, which represents a Date without time, similar to Java 8's LocalDate class.

How can compare date without time in SQL?

To compare dates without the time part, don't use the DATEDIFF() or any other function on both sides of the comparison in a WHERE clause. Instead, put CAST() on the parameter and compare using >= and < operators.

How do you compare only the date with moments?

We can use the isAfter method to check if one date is after another. We create a moment object with a date string. Then we call isAfter on it with another date string and the unit to compare.


1 Answers

Try compare dates changing to 00:00:00 its time (as this function do):

public static Date getZeroTimeDate(Date fecha) {
    Date res = fecha;
    Calendar calendar = Calendar.getInstance();

    calendar.setTime( fecha );
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    res = calendar.getTime();

    return res;
}

Date currentDate = new Date();// get current date           
Date eventDate = tempAppointments.get(i).mStartDate;
int dateMargin = getZeroTimeDate(currentDate).compareTo(getZeroTimeDate(eventDate));
like image 194
yoprogramo Avatar answered Oct 19 '22 07:10

yoprogramo