how to get date value from Jspinner excluding time

9.3k views Asked by At

i have a Jspinner which shows current date("yyyy//mm//dd" in this format) and i want to get only current date not time,when i use System.out.println(Spinner.getvalue()).It also shows time like this(Mon Sep 09 02:47:53 IST 2013).i just want current date 2013/09/08

2

There are 2 answers

0
dic19 On BEST ANSWER

From 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()

This means when you call getValue() method you'll get a java.util.Date object (since you're using SpinnerDateModel I guess). When you use System.out.println(Spinner.getvalue()) the output shows the default toString() implementation from java.util.Date class which is long and includes the time.

If you want to show the date in this format yyyy/mm/dd try this:

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

Note: don't get confused about java.util.Date object and its String representation.

0
Bolesław Denk On

It's seems that you need to use some date converting tool like SimpleDateFormat.

SimpleDateFormat formater = new SimpleDateFormat("yyyy/MM/dd");
String spinnerValue = formater.format(Spinner.getvalue());
System.out.println(spinnerValue);

All SimpleDateFormat info and date patterns can be found here: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html