Daniel Katz
Daniel Katz

Reputation: 2408

Java's JSpinner changes it's year wrongly

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));

Upvotes: 2

Views: 503

Answers (1)

Reimeus
Reimeus

Reputation: 159804

Try using y instead of Y

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

Y = Week Year, y = Year

Read: SimpleDateFormat

Upvotes: 2

Related Questions