0

我是android新手,任何人都可以发布以下场景的代码。

我必须为“from”和“to”设置日期并进行验证,即“to”日期应大于“from”,“from”日期应小于“to”日期。那可能吗?

提前致谢。

4

2 回答 2

1
public static boolean checkIfEndDateIsAfter(String startDate,String endDate)
{
    try
    {
        String myFormatString = "MM-dd-yy"; // for example
        SimpleDateFormat dateFormat = new SimpleDateFormat(myFormatString);
        Date endingDate = dateFormat.parse(endDate);
        Date startingDate = dateFormat.parse(startDate);

        if (date1.after(startingDate)) {
            return true;
        }
        else if (date1.equals(startingDate)) {
            return true; //some instances it is okay for it to be the same
        }
        else
            return false;
    }
    catch (Exception e)
    {

        return false;
    }
}
于 2015-02-22T03:49:41.873 回答
0

试试这个:

public class DatePickerFixedDates extends Activity {    

    final Calendar calender = Calendar.getInstance();
    int maxYear = calender.get(Calendar.YEAR);
    int maxMonth = calender.get(Calendar.MONTH);
    int maxDay = calender.get(Calendar.DAY_OF_MONTH);

    int minYear = 2004;
    int minMonth = 10; 
    int minDay = 25;
    private Button btn;
    private  DatePicker BirthDateDP;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.datepickerfixeddates);
        BirthDateDP = (DatePicker) findViewById(R.id.datepicker);
        btn = (Button) findViewById(R.id.button1);        
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {       
                int day = BirthDateDP.getDayOfMonth();
                int month = BirthDateDP.getMonth();
                int year = BirthDateDP.getYear();           
            }
        });

        BirthDateDP.init(maxYear , maxMonth, maxDay, new OnDateChangedListener() {

            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth)
            {           

                if (year < minYear)
                    view.updateDate(minYear, minMonth, minDay);

                    if (monthOfYear < minMonth && year == minYear)
                    view.updateDate(minYear, minMonth, minDay);

                    if (dayOfMonth < minDay && year == minYear && monthOfYear == minMonth)
                    view.updateDate(minYear, minMonth, minDay);


                    if (year > maxYear)
                    view.updateDate(maxYear, maxMonth, maxDay);

                    if (monthOfYear > maxMonth && year == maxYear)
                    view.updateDate(maxYear, maxMonth, maxDay);

                    if (dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth)
                    view.updateDate(maxYear, maxMonth, maxDay);
            }
        });        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
于 2013-02-06T05:20:43.943 回答