java 如何在 JDateChooser 中设置 JCalendar 日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29300122/
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-11-02 15:01:30  来源:igfitidea点击:

How to set JCalendar date in JDateChooser

javaswingjcalendarjdatechooser

提问by mfrancisp

I have a JDateChooser that is used for selecting birth dates. It starts out as null date value and let's the user choose the birth date either using the textfield or the jdatechooser button which opens the Jcalendar.

我有一个用于选择出生日期的 JDateChooser。它以空日期值开始,让用户使用文本字段或打开 Jcalendar 的 jdatechooser 按钮选择出生日期。

What I'm trying to do is to set the JCalendar's date to -10 years from the current date. By default, the JCalendar is setting the date at the current year. I tried this code but it doesn't work -- it still sets the date to the current date. Any help will do. Thanks!

我想要做的是将 JCalendar 的日期设置为当前日期的 -10 年。默认情况下,JCalendar 将日期设置为当前年份。我试过这段代码,但它不起作用——它仍然将日期设置为当前日期。任何帮助都可以。谢谢!

    dcBirthDateNew = new JDateChooser();
    dcBirthDateNew.setDateFormatString("MM/dd/yyyy");
    dcBirthDateNew.getDateEditor().getUiComponent().addFocusListener(new FocusAdapter() {
        @Override
        public void focusGained(FocusEvent evt) {
            if (evt.getSource() instanceof JTextComponent) {
                final JTextComponent textComponent=((JTextComponent)evt.getSource());
                SwingUtilities.invokeLater(new Runnable(){
                    public void run() {
                        textComponent.selectAll();
                    }});
            }   
        }
    });
    JCalendar calx = dcBirthDateNew.getJCalendar();
    calx.setCalendar(getPrevDate());

private static Calendar getPrevDate() {
    DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    Date myDate = new Date(System.currentTimeMillis());
    System.out.println("result is "+ dateFormat.format(myDate));
    Calendar cal = Calendar.getInstance();
    cal.setTime(myDate);
    cal.add(Calendar.YEAR, -10);
    System.out.println(dateFormat.format(cal.getTime()));
    return cal;
}

Screenshot

截屏

回答by trashgod

This seems to work correctly.

这似乎工作正常。

Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -10);

JDateChooser chooser = new JDateChooser(c.getTime());
this.add(chooser);

image

图片

Addendum: The popup editor's date defaults to the value in the JDateChooser; if you set it explicitly, it will become the default value:

附录:弹出编辑器的日期默认为JDateChooser; 中的值。如果您明确设置它,它将成为默认值:

JDateChooser chooser = new JDateChooser();
chooser.getDateEditor().setDate(c.getTime());

You can update another, empty text field in a PropertyChangeListener, as shown here; the property name is "date".

您可以在更新另一个空文本字段PropertyChangeListener,如在这里; 属性名称是"date".

Addendum: Looking closer, the JDateChooserimplementation of ActionListenerhandles the default date described above. You can override it, replacing the lines shown below, to create a different default.

附录:仔细观察,JDateChooser实现ActionListener处理上述默认日期。您可以覆盖它,替换下面显示的行,以创建不同的默认值。

JDateChooser chooser = new MyDateChooser();
this.add(chooser);
…
private static class MyDateChooser extends JDateChooser {

    @Override
    public void actionPerformed(ActionEvent e) {
        …
        Date date = dateEditor.getDate();
        if (date == null) {
            Calendar c = Calendar.getInstance();
            c.add(Calendar.YEAR, -10);
            calendar.setTime(c.getTime());
        } else {
            calendar.setTime(date);
        }
        jcalendar.setCalendar(calendar);
        …
    }
}

image

图片

回答by Cristian Marian

Maybe there is another way of doing this, but this is what I have:

也许还有另一种方法可以做到这一点,但这就是我所拥有的:

Date date = new Date();
Calendar calendar = Calendar.getInstance();
int subtractYearValue = 10;

int currentYear = Calendar.getInstance().get(Calendar.YEAR);
int currentMonth = Calendar.getInstance().get(Calendar.MONTH);
int currentDate= Calendar.getInstance().get(Calendar.DATE);

calendar.set(currentYear - subtractYearValue , currentMonth , currentDate);
date.setTime(calendar.getTimeInMillis());

dateChooser.setDate(date);