2

我试图限制日期和时间。我希望如果用户尝试将日期设置为小于当前日期,那么它应该显示警报,并且同样的事情是用时间来完成的。我正在使用日期和时间选择器,我的onDateSetListeneronTimeSetListener如下:

DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            dateTime.set(Calendar.YEAR, year);
            dateTime.set(Calendar.MONTH, monthOfYear);
            dateTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            update_date_display();



            // ---for setting notification----

            day = dayOfMonth;
            month = monthOfYear;
            AddEditTask.year = year;

            c.set(year, month, day);
        }
    };

    protected void update_date_display() {

        et_task_date.setText(formatDate.format(dateTime.getTime()));
    }

    TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            dateTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            dateTime.set(Calendar.MINUTE, minute);
            update_time_display();

            // ---for setting notification---

            hour = hourOfDay;
            AddEditTask.minute = minute;

            c.set(Calendar.HOUR_OF_DAY, hourOfDay);
            c.set(Calendar.MINUTE, minute);
            c.set(Calendar.SECOND, 0);
        }
    };

    private void update_time_display() {

        et_task_time.setText(formatTime.format(dateTime.getTime()));
    }

你能告诉我在这些听众中要写什么来实现我想要的吗?提前致谢...!!!

4

3 回答 3

8

以这种方式创建一个新的 Date 对象:

Date now = new Date(System.currentTimeMillis());

并使用int result = now.compareTo(theOtherDate);

如果 now Date 小于 theOtherDate Date,则结果将是 int < 0,如果它们相等,则为 0,如果此 Date 大于则 int > 0。

于 2013-06-16T10:06:44.970 回答
1

The code gets the user date from date picker and converting to java date format and storing the date in date format variable and then i'm getting the current system date and storing the date in another date format variable, now i got two date where i can easily compare.

DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
   public void onDateSet(DatePicker view, int myear, int monthOfYear,int dayOfMonth) {
// get the user picked date from date picker 
        year=myear;         
        month=monthOfYear+1;
        day=dayOfMonth; 
// then convert all values to a single string
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        String sday = (""+day);
        String smonth=("-"+month);
        String syear=("-"+year);
        String y=(""+sday+smonth+syear);
//convert the string date to a java date             
                    try 
        {
            Date pickerdate = formatter.parse(y);

        } catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//printing the system current date and picker date
       Date systemdate = new Date(System.currentTimeMillis());
       System.out.println("current "+systemdate);

       System.out.println("format"+pickerdate);

//now u got the two date in date type to compare it easily put ur if and else condition

                   if(your comparedate condition)
                    {
                         do what U want to do
                    }
                   else
                    {

                    }
         }
};
于 2014-02-21T14:37:32.877 回答
1
protected boolean checkDateValidity(Date date){
        Calendar cal = new GregorianCalendar(date.getYear() + 1900, date.getMonth(), date.getDate(), date.getHours(), date.getMinutes());
        if((cal.getTimeInMillis()-System.currentTimeMillis()) <= 0){
            return false;
        }else{
            return true;
        }
    }

将日期对象传递给此方法

如果它返回 false 表示您将显示警报消息

于 2014-01-08T12:13:19.987 回答