我有一个 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/