Reputation: 49329
I have created the following class that extends JSpinner
to iterate over dd/mm/yyy values.
public class DateSpinner extends JSpinner{
Calendar calendar = new GregorianCalendar();
public DateSpinner(){
super();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date now = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, -2);
Date startDate = calendar.getTime();
calendar.add(Calendar.YEAR, 100);
Date endDate = calendar.getTime();
SpinnerDateModel dateModel = new SpinnerDateModel
( now , startDate , endDate , Calendar.DAY_OF_MONTH );
setModel(dateModel);
JFormattedTextField tf =
((JSpinner.DefaultEditor)getEditor()).getTextField();
DefaultFormatterFactory factory =
(DefaultFormatterFactory)tf.getFormatterFactory();
DateFormatter formatter = (DateFormatter)factory.getDefaultFormatter();
// Change the date format to only show the hours
formatter.setFormat(new SimpleDateFormat("dd/MM/yyyy"));
}
}
My problem is when I set its value using
Date today = new Date();
spinner.setValue(today);
I get the date including the time and day of the week. If I touch the spinner it formats it according to the format I set. How can I initially get the format I want to be shown?
Upvotes: 0
Views: 1450
Reputation: 17775
Also, as a side note - whenever you work with calendars, dates and times I would consider using Joda Time.
Upvotes: 1
Reputation: 17359
Use built-in editors, such as following
spinner.setEditor(new JSpinner.DateEditor(spinner, "dd/MM/yyyy"));
Upvotes: 2