如何设置最短日期

时间:2016-03-07 08:25:39

标签: java android

我有两个按钮。第一个按钮 - 离开和第二个 - 到达。在出发日期,minDate =今天。到达日期minDate应该等于所选择的出发日期

onCreate():

final Calendar nextYear = Calendar.getInstance();
nextYear.add(Calendar.YEAR, 1);

Date today = new Date();
Date departs;

initButtonListeners(today, nextYear);

ButtonClickListener:

public void initButtonListeners(final Date today, final Calendar nextYear) {
    findViewById(id.linearCalendar).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showCalendarInDialog("select departure date", R.layout.calendar_dialog_customized);

                dialogView.init(today, nextYear.getTime())
                        .withSelectedDate(new Date(departData));

        }
    });

    findViewById(id.linearArrive).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showCalendarInDialog("select arrival date", R.layout.calendar_dialog_customized);

            Date dates = null;

                SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy");
                try {
                    dates = format.parse(departData);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                dialogView.init(dates, nextYear.getTime())
                        .withSelectedDate(new Date(arriveData));
            }

        }
    });

}

OnDateClickListener:

dialogView.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
        DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy");

        @Override
        public void onDateSelected(Date date) {
            if(isFrom){
                departData = dateFormat.format(date);
                tvDepart.setText(departData);
            }else{
                arriveData = dateFormat.format(date);
                tvArrive.setText(arriveData);
            }

            theDialog.dismiss();

        }

        @Override
        public void onDateUnselected(Date date) {

        }

    });

点击到达按钮时,应用会显示零值

错误:

java.lang.IllegalArgumentException: The string argument is null
 at java.util.Date.parse(Date.java:373)
 at java.util.Date.<init>(Date.java:149)
 at com.tezz.MainContent.ContentActivity$2.onClick(ContentActivity.java:305)
 at android.view.View.performClick(View.java:4084)
 at android.view.View$PerformClick.run(View.java:16966)
 at android.os.Handler.handleCallback(Handler.java:615)
 at android.os.Handler.dispatchMessage(Handler.java:92)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:4745)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:511)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 at dalvik.system.NativeStart.main(Native Method)

什么是不对的,如何放入minDate?

2 个答案:

答案 0 :(得分:0)

try this 
if (event.value)
    {
      // Convert field value to a date
         var d = util.scand("mmddyyyy", event.value);
    // Set maximum date
         var d_max = new Date(2013, 11, 31, 23, 59, 59);
    // Set minimum date
        var d_min = new Date(2010, 11, 31, 23, 59, 59);
        if (d.getTime()>d_max.getTime() || d.getTime()<d_min.getTime()) {
        // Reject the entry
             event.rc = false;
              // Optionally, alert the user
                  app.alert("Message here.");
    }
}

答案 1 :(得分:0)

所选日期应为今天的日期,因为 departData 将为空。

findViewById(id.linearCalendar).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showCalendarInDialog("select departure date", R.layout.calendar_dialog_customized);

            dialogView.init(today, nextYear.getTime())
                    .withSelectedDate(new Date());

    }
});