I Have created a JSpinner object that moves up and down daily, and skips weekends when clicked on. I then added a feature that if you open the program during a Saturday or Sunday it should roll back to Friday. The issue is rather than changing to Friday the JSpinner jumps back one week.
I tried isolating the issue in a new program and have had no success. Code should be the exact same so I am not sure what is causing this issue.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
DecimalFormat df = new DecimalFormat("#.00");
SpinnerDateModel spinnerModel;
JSpinner spinner;
Integer loopStep;
Integer firstloopStep = 0;
MainFrame() {
super("Spinner Control");
setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
JPanel panelNorth = new JPanel();
// spinner for date information
spinnerModel = new SpinnerDateModel();
spinnerModel.setCalendarField(Calendar.WEEK_OF_MONTH);
spinner = new JSpinner(spinnerModel);
JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, "EEEEE, dd MMMMM, yyyy");
spinner.setEditor(editor);
// add panels/buttons/etc to page
add(panel1, BorderLayout.PAGE_START);
panel1.setPreferredSize(new Dimension(1280, 100));
panelNorth.setPreferredSize(new Dimension(1280, 40));
panel1.add(panelNorth, BorderLayout.NORTH);
panelNorth.add(spinner, BorderLayout.PAGE_START);
//method executes on start up
skipWeekends();
loopStep = 0;
//rotate the date, skip weekends, works fine here
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent arg0) {
Date date = (Date) spinner.getValue();
System.out.println("todays date " + date);
SimpleDateFormat sdf = new SimpleDateFormat("EEEEE, dd MMMMM, yyyy");
String stringDate = sdf.format(date);
System.out.println(stringDate);
char c = stringDate.charAt(0);
char c2 = stringDate.charAt(1);
//skip the weekends
if ((c == 'S') && (c2 == 'a') && (loopStep == 0)) {
loopStep = 1;
spinner.setValue(spinner.getNextValue());
spinner.setValue(spinner.getNextValue());
System.out.println("Saturday Spinner going back 2");
}
if ((c == 'S') && (c2 == 'u') && (loopStep == 0)) {
loopStep = 1;
spinner.setValue(spinner.getPreviousValue());
spinner.setValue(spinner.getPreviousValue());
System.out.println("Sunday Spinner going back 2");
}
if (loopStep == 1) {
loopStep = 2;
}
if (loopStep == 2) {
loopStep = 0;
}
}
});
setExtendedState(MAXIMIZED_BOTH);
setMinimumSize(new Dimension(1280, 485));
setSize(640, 485);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@PostConstruct
private void skipWeekends(){
//skips weekends when program opens, skips weeks for some reason
Date date = (Date) spinner.getValue();
System.out.println("todays date " + date);
SimpleDateFormat sdf = new SimpleDateFormat("EEEEE, dd MMMMM, yyyy");
String stringDate = sdf.format(date);
System.out.println(stringDate);
char c = stringDate.charAt(0);
char c2 = stringDate.charAt(1);
if ((c == 'S') && (c2 == 'a')) {
spinner.setValue(spinner.getPreviousValue());
System.out.println("Saturday Spinner going back 2");
}
if ((c == 'S') && (c2 == 'u')) {
spinner.setValue(spinner.getPreviousValue());
spinner.setValue(spinner.getPreviousValue());
System.out.println("Sunday Spinner going back 2");
}
}
}
You're setting the SpinnerDateModel's calendar field to Calendar.WEEK_OF_MONTH instead of Calendar.DAY_OF_WEEK.
When using the Spinner buttons, this gets corrected silently, because the Spinner will set the calendar field before it commits the change (this is mentioned in the SpinnerDateModel documentation). Your @PostConstruct method runs before this had a chance to happen, though.
If I may add another suggestion: your way of figuring out the current day of the week is quite complicated and really brittle (it would break in Germany for example, because "Sunday" is "Sonntag" here). Here's an alternative. Call it with initial == true from the constructor and initial == false from stateChanged().
private Calendar cal = new GregorianCalendar();
private void skipWeekends(boolean initial) {
cal.setTime(spinnerModel.getDate());
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SATURDAY:
cal.add(Calendar.DAY_OF_WEEK, initial ? -1 : 2);
break;
case Calendar.SUNDAY:
cal.add(Calendar.DAY_OF_WEEK, -2);
break;
}
spinnerModel.setValue(cal.getTime());
}