gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 01:56:33 25 4
gpt4 key购买 nike

我在 JCalendar 中使用 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");

你已经在做 gthis 了吗?然后你必须发布你从组件中获取值(value)的地方。

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

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

myDateChooser.addPropertyChangeListener(new PropertyChangeListener() {

@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("date")) {
dateString = new SimpleDateFormat("yyyy-MM-dd").format(myDateChooser.getDate());
}
}
});

关于java - 在 JCalendar 中使用 JDateChooser 不需要的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7639214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com