Java 在JCalendar中使用JDateChooser不需要的数据

Java 在JCalendar中使用JDateChooser不需要的数据,java,swing,jcalendar,jdatechooser,Java,Swing,Jcalendar,Jdatechooser,我在中使用JDateChooser(带Swing)。我试图得到一个“yyyy-MM-dd”格式,但由于某些原因,我也得到了时间,并且它总是相同的(00:00:00 MDT)。有人知道如何打发时间吗? 提前谢谢 try { calendarDate = new JDateChooser(); } catch (Exception e) { e.printStackTrace(); } calendarDate.setDateFormatString("yyyy-MM-dd");

我在中使用JDateChooser(带Swing)。我试图得到一个“yyyy-MM-dd”格式,但由于某些原因,我也得到了时间,并且它总是相同的(00:00:00 MDT)。有人知道如何打发时间吗? 提前谢谢

try {
    calendarDate = new JDateChooser();
} catch (Exception e) {
    e.printStackTrace();
}
calendarDate.setDateFormatString("yyyy-MM-dd");

dateLabel = new JLabel("Date");
parent.frame2.getContentPane().add(dateLabel);//1
parent.frame2.getContentPane().add(calendarDate);   

要获得
JDateChooser
以显示特定的日期格式,您需要使用其
setDateFormatString
API设置该特定格式

例如:

JDateChooser myDateChooser = new JDateChooser();
myDateChooser.setDateFormatString("yyyy-MM-dd");
你已经在做这个了吗?然后,您必须张贴从组件获取价值的位置

在处理JDateChooser属性更改的地方,可以执行以下操作以获得相同格式的日期:

示例:(假设字符串dateString是需要日期字符串的位置)


我使用的是源代码,而不是*.jar中的编译文件

calNewDate.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
calNewDate.setSpiningCalendarField(Calendar.DAY_OF_MONTH);
calNewDate.setFont(new Font("SansSerif", Font.BOLD, 12));
calNewDate.setBackground(someColor);
calNewDate.addChangeListener(new ChangeListener() {

    @Override
    public void stateChanged(ChangeEvent e) {
       //some stuff
    }
});
calNewDate.setToolTipText("Whatever");

请注意格式化JDateChososer文本字段中的日期戳。一个常见的词汇错误是,我假设JDateChooser后面有一个
addPropertyChangeListener
,用于捕获用户在输入中设置的日期:

dateInserted.getDateEditor().addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                // TODO Auto-generated method stub
                if ("date".equals(evt.getPropertyName())) {
                    Date date = dateInserted.getDateEditor().getDate();
                    ordine.setOrderDate(date);
                    dateInserted.setDateFormatString("dd/MM/YYYY");
                    dateInserted.setDate(date);
                    System.out.println(date.toString());
                    dateInserted.setBorder(BorderFactory.createLineBorder(Color.GREEN));
if (canIenableCalcolaEAggiungi(2) == true)
                        calculatingAndAdding.setEnabled(true);
                    else {
                        calculatingAndAdding.setEnabled(false);
                    }
                }
            }
        });
脚本中的内容写错了:

 dateInserted.setDateFormatString("dd/MM/YYYY");

当您选择年份的格式模式“yyy”(而不是“yyy”或“yy”cfr API)时,它将无法识别,并生成如下故障:当您尝试编辑JDateChooser的文本字段并使鼠标移动时,日期会自动以随机值更改。不能再改变了。这也会产生一个不想要的日期。

注意,最新版本是1.4,日期是2011年9月7日。你说的“我也有时间”是什么意思?你在哪里看到这次展览?@PaulKar。在您的评论后编辑了答案谢谢您使用这种“新的SimpleDataFormat(“yyyy MM dd”).format(myDateChooser.getDate()@Paul:我在
PropertyChangeListener
中得到了预期的结果,使用您的格式“yyyy MM dd”;设置标签时可能需要应用相同的格式。
 dateInserted.setDateFormatString("dd/MM/YYYY");