我创建了以下类,该类扩展JSpinner
为迭代 dd/mm/yyy 值。
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"));
}
}
我的问题是当我使用
Date today = new Date();
spinner.setValue(today);
我得到日期,包括时间和星期几。如果我触摸微调器,它会根据我设置的格式对其进行格式化。我最初如何获得我想要显示的格式?