从 JSpinner (SWING) 获取价值 [英] Getting the value from JSpinner (SWING)
本文介绍了从 JSpinner (SWING) 获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从 JSpinner 获取价值.
I need to get value from JSpinner.
private JSpinner[] timeSpinner;
private SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date time = new Date();
try {
time = format.parse(timeSpinner[i].getValue().toString());
} catch (ParseException e) {
e.printStackTrace();
}
data[i] = time.toString();
System.out.println(data[i]);
错误信息:
java.text.ParseException: Unparseable date: "Thu Jan 01 01:05:00 CET 1970"
推荐答案
您正在创建的 SimpleDateFormat 与您尝试解析的日期格式不同:
The SimpleDateFormat you are creating has a different format from the date you are trying to parse:
new SimpleDateFormat("HH:mm:ss");
Thu Jan 01 01:05:00 CET 1970
实际上再看一遍,您似乎已经有了一个 Date 对象,为什么不直接使用它呢?
Actually looking again it looks like you already have a Date object why not just use that?
time = (Date)timeSpinner[i].getValue();
<小时>
为了将您拥有的日期转换为字符串,您只需执行上述操作即可获取日期,然后执行
In order to turn the date you have into a String you just have to do the above to get a Date and then do
String formattedDate = format.format(time);
这篇关于从 JSpinner (SWING) 获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文