Question

This is how I created a date JSpinner. When I click on arrows, it can only decrement year by some value (two or one) or increment (only couple of times) by one. I am confused, please help!

Date current = calendar.getTime();
SpinnerDateModel yearsData = new SpinnerDateModel(current,null,null,Calendar.YEAR);
JSpinner years = new JSpinner(yearsData);
years.setEditor(new JSpinner.DateEditor(years, "YYYY"));
JFormattedTextField tfYears = ((JSpinner.DefaultEditor)years.getEditor()).getTextField();
tfYears.setHorizontalAlignment(JTextField.RIGHT);
tfYears.setFont(new Font("SansSerif", Font.PLAIN, 10));
Was it helpful?

Solution

Try using y instead of Y

years.setEditor(new JSpinner.DateEditor(years, "yyyy"));

Y = Week Year, y = Year

Read: SimpleDateFormat

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top