比较用户选择的日期和系统时间

时间:2015-07-24 14:08:49

标签: java android

如何在onSelectedDayChange()中比较系统日期与用户选择的日期?如果用户选择过去的日期,则发出提醒,否则我想转到下一个活动。

我的代码是:

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();
    }

}

1 个答案:

答案 0 :(得分:0)

使用日历代替并以时间比较时间。

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

顺便说一句。你应该使用日历。 :)