java 如何使用 com.google.gwt.user.datepicker.client.DateBox 限制可用的日期范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5656495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:07:29  来源:igfitidea点击:

How to limit the available Date ranges with the com.google.gwt.user.datepicker.client.DateBox

javagwtdatepickergwt2datebox

提问by

I need to limit what Datesa user can pick from the com.google.gwt.user.datepicker.client.DateBox.

我需要限制Dates用户可以从com.google.gwt.user.datepicker.client.DateBox.

I can't seem to figure out how to limit the min Dateso they can't pick past dates.

我似乎无法弄清楚如何限制 minDate以便他们无法选择过去的日期。

If I can't do this with com.google.gwt.user.datepicker.client.DateBoxis there an alternative DateBoxwidget that will allow me this kind of flexibility?

如果我不能这样做,com.google.gwt.user.datepicker.client.DateBox是否有替代DateBox小部件可以让我具有这种灵活性?

回答by

Based on the suggestions I received, here is what I came up with that works on limiting the selectable dates to only the current day and after. This works on GWT 2.1.1

根据我收到的建议,这是我想出的将可选日期限制为当天和之后的方法。这适用于 GWT 2.1.1

final DateBox dateBox = new DateBox();
dateBox.addValueChangeHandler(new ValueChangeHandler<Date>()
{
    @Override
    public void onValueChange(final ValueChangeEvent<Date> dateValueChangeEvent)
    {
        if (dateValueChangeEvent.getValue().before(today()))
        {
            dateBox.setValue(today(), false);
        }
    }
});
dateBox.getDatePicker().addShowRangeHandler(new ShowRangeHandler<Date>()
{
    @Override
    public void onShowRange(final ShowRangeEvent<Date> dateShowRangeEvent)
    {
        final Date today = today();
        Date d = zeroTime(dateShowRangeEvent.getStart());
        while (d.before(today))
        {
            dateBox.getDatePicker().setTransientEnabledOnDates(false, d);
            d = nextDay(d);
        }
    }
});

And for completeness here are the statichelper methods for manipulating the dates:

为了完整起见,这里是static操作日期的辅助方法:

private static Date today()
{
    return zeroTime(new Date());
}

/** this is important to get rid of the time portion, including ms */
private static Date zeroTime(final Date date)
{
    return DateTimeFormat.getFormat("yyyyMMdd").parse(DateTimeFormat.getFormat("yyyyMMdd").format(date));
}

private static Date nextDay(final Date date)
{
    return zeroTime(new Date(date.getTime() + 24 * 60 * 60 * 1000));
}

回答by dafunkeemonkee

I am guessing here but you may need to use the addValueChangeHandler. An example:

我在这里猜测,但您可能需要使用 addValueChangeHandler。一个例子:

datebox.addValueChangeHandler(new ValueChangeHandler<Date>() {
    public void onValueChange(ValueChangeEvent<Date> event) {
        Date date = event.getValue();
        if (date.before(new Date())) {
         fromDatePicker.setValue(new Date());
        }
    }
});

Here when a user somehow selects a date that is in the past, it will auto set it to current date (or whatever fits your needs).

在这里,当用户以某种方式选择过去的日期时,它会自动将其设置为当前日期(或任何适合您需要的日期)。