how do i compare current date with user input date from date picker

萝らか妹 提交于 2019-12-09 01:38:07

问题


I am trying to keep constraint on date and time. I want that if user tries to set date less than current date then it should show alert, and same thing is to be done with time. I am using date and time pickers and my onDateSetListener and onTimeSetListener are as below:

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

can you please tell me what is to be written within those listeners to achieve what i want. Thanks in advance...!!!


回答1:


Create a new Date object this way:

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

and use int result = now.compareTo(theOtherDate);

result will be an int < 0 if now Date is less than the theOtherDate Date, 0 if they are equal, and an int > 0 if this Date is greater.




回答2:


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

pass the date object to this method

if it returns false means you will show alert message




回答3:


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
                    {

                    }
         }
};


来源:https://stackoverflow.com/questions/17132106/how-do-i-compare-current-date-with-user-input-date-from-date-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!