java - 如何从 Jspinner 获取不包括时间的日期值

标签 java jspinner

我有一个 Jspinner 显示当前日期(“yyyy//mm//dd”这种格式),当我使用 System.out.println(Spinner.getvalue() 时,我只想获取当前日期而不是时间)。它还显示这样的时间(Mon Sep 09 02:47:53 IST 2013)。我只想要当前日期 2013/09/08

最佳答案

来自JSpinner javadoc :

public Object getValue()

Returns the current value of the model, typically this value is displayed by the editor. If the user has changed the value displayed by the editor it is possible for the model's value to differ from that of the editor, refer to the class level javadoc for examples of how to deal with this.

This method simply delegates to the model. It is equivalent to:

getModel().getValue()

这意味着当您调用 getValue() 方法时,您将获得一个 java.util.Date 对象(因为您使用的是 SpinnerDateModel > 我猜)。当您使用 System.out.println(Spinner.getvalue()) 时,输出显示来自 java.util.Date 的默认 toString() 实现> 类(class)很长并且包含时间。

如果您想以这种格式显示日期yyyy/mm/dd,请尝试以下操作:

System.out.println(new SimpleDateFormat("yyyy/MM/dd").format(spinner.getValue()));

注意:不要对 java.util.Date 对象及其 String 表示形式感到困惑。

关于java - 如何从 Jspinner 获取不包括时间的日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18704861/

相关文章:

java - 打开 ubuntu 终端并运行终端命令,例如 "`./executable -x -y`“形成 java 代码

java - 如何在 LibSVM 中使用 'svm_toy' Applet 示例?

java - 如何使 JSpinner 对象的全部内容突出显示,以便在获得焦点时用户输入不需要删除?

java - JSpinner 在 JOptionPane 中占据整个宽度

java - 使 JSpinner 在获得焦点时选择文本

java - 无法将 ChangeListener 添加到 JSpinner

java - Swing 改变 BasicArrowButton 的前景色

java - 当我尝试在 Eclipse 上创建新的 java 项目时没有任何反应

Java CardLayout : Is it possible to use same object on two different cards?

Java JSON对象解析