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

前端 未结 3 606
深忆病人
深忆病人 2021-01-07 02:08

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 ti

相关标签:
3条回答
  • 2021-01-07 02:32

    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
                        {
    
                        }
             }
    };
    
    0 讨论(0)
  • 2021-01-07 02:35

    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.

    0 讨论(0)
  • 2021-01-07 02:41
    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

    0 讨论(0)
提交回复
热议问题