gpt4 book ai didi

java - 如何从jspinner获取日期值

转载 作者:太空宇宙 更新时间:2023-11-04 13:41:44 25 4
gpt4 key购买 nike

我搜索了一段时间,无法从jspinner中获取值。这是我的代码:

Calendar calendar = Calendar.getInstance();
int currentYear;
currentYear = calendar.get(Calendar.YEAR);

SpinnerModel model =
new SpinnerNumberModel(currentYear, //initial value
currentYear - 100, //min
currentYear + 100, //max
1); //step


Date initDate = calendar.getTime();
calendar.add(Calendar.YEAR, -100);
Date earliestDate = calendar.getTime();
calendar.add(Calendar.YEAR, 200);
Date latestDate = calendar.getTime();
model = new SpinnerDateModel(initDate,
earliestDate,
latestDate,
Calendar.YEAR);
final JSpinner spinnerDateNais = new JSpinner(model);
spinnerDateNais.setEditor(new JSpinner.DateEditor(spinnerDateNais, "dd/MM/yyyy"));

Date dateTest;
spinnerDateNais.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
System.out.println((Date)spinnerDateNais.getValue());//Here I need to get the values
}
});

panneauFormulaire.add(spinnerDateNais);

所以我需要获取日期jspinner的值并使用它。你有什么想法吗?

最佳答案

试试这个。

初始化 JSpinner 代码

JSpinner DateSpinner = new JSpinner(new SpinnerDateModel()); 
JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(DateSpinner, "E dd/MM/yyyy");
DateSpinner.setEditor(dateEditor);

从 JSpinner 获取 LocalDate 的代码

Date date = (Date) DateSpinner.getValue();   
LocalDate localDate = DateConverter.asLocalDate(date);

关于java - 如何从jspinner获取日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31189811/

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