目前,我正在使用带有SpinnerDateModel和JSpinner的java swing应用程序来设置时间。下面是我的代码:
shiftTime = new JSpinner(new SpinnerDateModel());
JSpinner.DateEditor de_shiftTime = new JSpinner.DateEditor(shiftTime,
"HH:mm:ss");
shiftTime.setEditor(de_shiftTime);
//shiftTime.setValue(new Date()); // will only show the current time
shiftTime.setSize(108, 22);
shiftTime.setLocation(436, 478);
add(shiftTime);
但是,当我使用.getValue()方法将选定的时间添加到我的数据库(我使用的是mySql)时,时间将与日期一起添加。我不想要日期,只是时间,例如13:59:16。然而,我现在得到的是“清华一月01 13:59:16中士1970年”。
是否有一种方法可以重写新的SpinnerDateModel(),或者删除结果中的日期?任何帮助都是非常感谢的!谢谢!:-)
发布于 2014-08-03 06:15:36
您可以使用SimpleDateFormat
将其转换为所需的输出:
Date date=(Date)shiftTime.getValue();
SimpleDateFormat format=new SimpleDateFormat("HH:mm:ss");
System.out.println(format.format(date));
JSpinner.getValue
返回Object
,而您只是打印默认Date#toString()
返回的值。
发布于 2014-08-03 06:26:29
如果您使用的是尤达-时间 --而且您应该使用java日期库,那么您的代码将看起来像:
DateTime date = new DateTime((Date)shiftTime.getValue());
ISODateTimeFormatter fmt = ISODateTimeFormatter.timeNoMillis();
String dateAsString = fmt.print(date);
System.err.println(dateAsString);
如果你需要进一步的帮助,请留下评论。
https://stackoverflow.com/questions/25102149
复制相似问题