0

How to compare system date with user selected date in onSelectedDayChange()? If user selects a date in the past then give an alert, otherwise I want to move to the next activity.

My code is:

public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
    //String sday = (""+dayOfMonth);
    //String smonth=("-"+month);
    //String syear=("-"+year);
     s = ("" + dayOfMonth + " : " + (month + 1) + " : " + year);

    try {
        strDate = formatter.parse(s);
        Date systemdate = new Date(System.currentTimeMillis());

        if (systemdate.compareTo(strDate)>0) {
            Toast.makeText(getApplication(), "wrong date", Toast.LENGTH_LONG).show();
        }

        else {
            Intent in = new Intent(MainActivity.this, sec.class);
            s = +dayOfMonth + " : " + (month + 1) + " : " + year;
            in.putExtra("TextBox", s.toString());
            startActivity(in);
            Toast.makeText(getBaseContext(), "Selected Date is\n\n" + dayOfMonth + " : " + (month + 1) + " : " + year,
                    Toast.LENGTH_SHORT).show();
        }
    } catch (ParseException e) {
       e.printStackTrace();
    }

}
Sebi
  • 1,390
  • 1
  • 13
  • 22
Soni Kumar
  • 121
  • 1
  • 2
  • 8

1 Answers1

0

Use Calendar instead and compare time in milis.

Calendar cal = Calendar.getInstance(); cal.set(year, month + 1, dayOfMonth); if(cal.getTimeInMilis() > System.getCurrentTimeMilis())

btw. You should use Calendar. :)

Lidjan
  • 154
  • 1
  • 2
  • 18