The problem of comparing dates (times) in Android

I have a problem when I try to compare two dates in Android. I'm not sure if this is a problem with the emulator or I have a problem with the code itself. The fact is that the code works in a normal Java software environment, which confuses me even more.

I have the following code for comparing dates in Android 2.1:

public boolean compareDates(String givenDateString) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    boolean True;
    try {
        True = false;
        Date givenDate = sdf.parse(givenDateString);
        Date currentDate = new Date();

        if(givenDate.after(currentDate)){
            True = true;
        } if(givenDate.before(currentDate)){
            True = false;
        } if(givenDate.equals(currentDate)){
            True = false;
        }   
    } catch (Exception e) {
        Log.e("ERROR! - comparing DATES", e.toString());
    }
    return True;
}

Now the code works well in Java, but in Android it returns me a lie. The only change happens when I insert the variable currentDate into a string like this:

Date currentDate = sdf.parse("16:50");

If the currentDate variable is set in the string, it returns true when I compare it with a value that is after the specified time. I also tried setting the currentDate variable with:

  Calendar calendar = Calendar.getInstance();
  Date currentDate = calendar.getTime();

I do not understand here at all. Hoping someone has ideas on what might be the problem here.

--- EDIT ---

. , , . :

public boolean compareDates(String givenDateString) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    boolean True = false;
    try {
        Date givenDate = sdf.parse(givenDateString);
        Calendar now = Calendar.getInstance();
            int hour = now.get(Calendar.HOUR_OF_DAY);
            int minute = now.get(Calendar.MINUTE);
        Date currentDate = sdf.parse(hour + ":" + minute);

        if(givenDate.after(currentDate)){
            True = true;
        } if(givenDate.before(currentDate)){
            True = false;
        } if(givenDate.equals(currentDate)){
            True = false;
        }   
    } catch (Exception e) {
        Log.e("ERROR! - comparing DATES", e.toString());
    }
    return True;
}
+2
1

, Calendar. compareTo():

if ( myCalendar.compareTo(upperLimitCalendar) >= 0 )

:

http://developer.android.com/reference/java/util/Calendar.html

0

All Articles