0

如何在 Android 中检查/验证结束日期 [文本框] 是否大于开始日期 [文本框]?

我已经在初始时间显示了当前日期。到运行时间,我将更改开始日期和结束日期。

更改开始日期时,结束日期必须大于开始日期...

帮我实现这个..

4

2 回答 2

7

试试这个功能。

public static boolean isDateAfter(String startDate,String endDate)
    {
        try
        {
            String myFormatString = "yyyy-M-dd"; // for example
            SimpleDateFormat df = new SimpleDateFormat(myFormatString);
            Date date1 = df.parse(endDate));
            Date startingDate = df.parse(startDate);

            if (date1.after(startingDate))
                return true;
            else
                return false;
        }
        catch (Exception e) 
        {

            return false;
        }
    }

如果 enddate 晚于开始日期,则返回 true。您可以根据需要更改日期格式。

于 2012-12-03T06:12:16.613 回答
0
private void initViewValue(){
    newCalendar = Calendar.getInstance();
    dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);

}
private void initViewEvents(){

    btn_start_date.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fromDate();
        }
    });

    btn_end_date.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            toDate();
        }
    });

}





private String  changeDate(String strStartDate){
    SimpleDateFormat readFormat = null, writeFormat = null;
    Date startDate = null, endDate = null;
    //  String strStartDate = "2014-12-09";

    readFormat = new SimpleDateFormat("dd-MM-yyyy");
    writeFormat = new SimpleDateFormat("MM/dd/yyyy");

    try {
        startDate = readFormat.parse(strStartDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return writeFormat.format(startDate);
}





private void fromDate(){
    fromDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            et_start_date.setText(dateFormatter.format(newDate.getTime()));

            getFromDate = et_start_date.getText().toString().trim();
        }
    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    fromDatePickerDialog.show();
}
private void toDate(){
    toDatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            et_end_date.setText(dateFormatter.format(newDate.getTime()));
            newDate.add(Calendar.DAY_OF_MONTH, 30);

        }

    },
            newCalendar.get(Calendar.YEAR), newCalendar.get(`enter code here`Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    toDatePickerDialog.getDatePicker().setMinDate(milliseconds(changeDate(getFromDate)));
    toDatePickerDialog.show();
}

public static long milliseconds(String date)
{
    //String date_ = date;
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
    System.out.println("Date in milli :: " + date);
    try
    {
        Date mDate = sdf.parse(date);
        long timeInMilliseconds = mDate.getTime();
        System.out.println("Date in milli :: " + timeInMilliseconds);
        return timeInMilliseconds;
    }
    catch (ParseException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return 0;
}
于 2017-05-19T12:45:22.727 回答