-1

I need to validate 2 date, one is picked by the user and the other is the one that I get form the calendar... this is what I try so far without success:

Calendar c = Calendar.getInstance();
if(!DateIsOlder(txtVwAppointmentDate.getText(), c.getTime())) {
            try {
                  //My code
                }  

public static boolean DateIsOlder(CharSequence date1, Date date2){
    //SimpleDateFormat Date2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz -HH:mm yyyy", Locale.ENGLISH);
    SimpleDateFormat dfDate = new SimpleDateFormat("dd/mm/yyyy", Locale.ENGLISH);
    boolean isOlder = false;
    try{
        //Date newDate = (Date)Date2.parse(date2);
        if(dfDate.parse(String.valueOf(date1)).before(dfDate.format(date2))){
        isOlder = true;
        }
        else if (dfDate.parse(String.valueOf(date1)).equals(dfDate.format(date2))){
        isOlder = false;
        }
        else {
            isOlder = false;
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return isOlder;
}

The error is on .before(dfDate.format(date2)) , it says : Error:(484, 51) error: method before in class Date cannot be applied to given types; required: Date found: String reason: actual argument String cannot be converted to Date by method invocation conversion

and if you can see, on the else if I compare both dates if they are equal and compiler do not show any error on this line:

else if (dfDate.parse(String.valueOf(date1)).equals(dfDate.format(date2)))

Also I try to change it to Date but no successs... any Idea to help me to solve this issue? thanks.

jugger
  • 1
  • 6
  • why do you do `dfDate.format(date2)`? – njzk2 Feb 17 '15 at 21:46
  • hi @njzk2 I do it to change the format of the date, because the calendar bring me the whole date like this "EEE MMM dd HH:mm:ss zzz -HH:mm yyyy" so I try to change in the format that the user pick that is in this format "dd/mm/yyyy" – jugger Feb 17 '15 at 21:52
  • read the prototype of the `before` method. – njzk2 Feb 17 '15 at 22:27

4 Answers4

0

You just can convert the users date into timestamp and can compare them.

DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
Date date = (Date)formatter.parse(str_date); 
Timestamp timeStamp = new TimeStamp(date);
Vikas Chandra
  • 565
  • 1
  • 9
  • 22
  • Hi @Vikas, the timestamp is also incompatible to compare with the methods before, after or equal – jugger Feb 17 '15 at 22:27
  • Timestamp is integer. Convert both the dates to the timestamp and compare like you compare two integers you will get it. – Vikas Chandra Feb 18 '15 at 09:15
0

Don't use java.util.Date. Google says the following about Date:

This class' deprecated display-related functionality is now provided by DateFormat, and this class' deprecated computational functionality is now provided by Calendar.

In other words, it's deprecated. You should be using DateFormat, which you can read more about here: http://developer.android.com/reference/java/text/DateFormat.html

To do date comparisons, you then want to make two different Calendar objects and call either before() or after()

Al Wang
  • 354
  • 2
  • 10
  • Hi @Al Wang, the android studio don't said anything about date being deprecated but i think I will use your suggestion. – jugger Feb 17 '15 at 22:26
0

Your problem is that dfDate.format(date2) returns a String, not a Date object, so you need to chage yout method to:

    public static boolean DateIsOlder(CharSequence date1, Date date2){
    SimpleDateFormat dfDate = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
    boolean isOlder = false;

    try{
        if(dfDate.parse(String.valueOf(date1)).before(dfDate.parse(dfDate.format(date2)))){
            isOlder = true;
        }
        else {
            isOlder = false;
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return isOlder;
}
alcaamado
  • 308
  • 3
  • 13
  • Hi @Adrián, I also try doing that, but debugging the code it just pass directly, it's like the dfDate.parse(dfDate.format(date2)) do nothing, it just bring the date and do not format it, for example look at this test a few seconds ago... dfDate.parse(dfDate.format(date2))= Sat Jan 17 00:16:00 GMT-05:00 2015 – jugger Feb 17 '15 at 22:17
  • I know it, but if you only want to compare if a date is before another date, you don't need to format them. When i tried the above code, it works for me. – alcaamado Feb 17 '15 at 22:22
  • Another question, look that in SimpleDateFormat, when you use 'mm', it means minutes, and 'MM' means months. I change it at the code that I post earlier, but I don't know if you change that at yours. – alcaamado Feb 17 '15 at 22:32
0

Well thanks to all of you who help me find a solution, I try to make the easy way and it works, I do not need all the information calendar.getTime brings so I do the following:

Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        if(!DateIsOlder(txtVwAppointmentDate.getText(), month + "/" + day + "/" + year)) {
            try { //my code }

public static boolean DateIsOlder(CharSequence date1, String date2){
    SimpleDateFormat dfDate = new SimpleDateFormat("MM/dd/yyyy");
    boolean isOlder = false;
    try{
        //Date newDate = (Date)Date2.parse(date2);
        if(dfDate.parse(String.valueOf(date1)).before(dfDate.parse(date2))){
            isOlder = true;
        }
        else {
            isOlder = false;
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return isOlder;
}

and it works without any problem! thanks to you all :)

jugger
  • 1
  • 6