Reputation: 2997
JDatePicker is an open source Java GUI component to choose dates
http://sourceforge.net/projects/jdatepicker/
There seems to be a bug when I set February as the month right after creating the component. All the other months work correctly.
package jat.examples.DatePicker;
import jat.jdatepicker.JDateComponentFactory;
import jat.jdatepicker.JDatePicker;
import javax.swing.JApplet;
import javax.swing.JComponent;
public class DatePickerExample extends JApplet{
private static final long serialVersionUID = 1920676464239324135L;
JDatePicker depart_date_picker;
public void init() {
depart_date_picker = JDateComponentFactory.createJDatePicker();
depart_date_picker.setTextEditable(true);
depart_date_picker.setShowYearButtons(true);
add((JComponent) depart_date_picker);
}
public void start() {
depart_date_picker.getModel().setYear(2010);
depart_date_picker.getModel().setMonth(1);
//depart_date_picker.getModel().setMonth(1);
depart_date_picker.getModel().setDay(15);
depart_date_picker.getModel().setSelected(true);
}
}
Instead of showing February, it shows March.
In the debugger, I noticed that oldValue is null.
public void setMonth(int month) {
int oldMonthValue = this.calendarValue.get(Calendar.MONTH);
T oldValue = getValue();
calendarValue.set(Calendar.MONTH, month);
fireChangeEvent();
firePropertyChange("month", oldMonthValue, this.calendarValue.get(Calendar.MONTH));
firePropertyChange("value", oldValue, getValue());
}
Sure enough, when I call the method twice, it shows February correctly.
depart_date_picker.getModel().setMonth(1);
depart_date_picker.getModel().setMonth(1);
Probably a variable initialization problem. Am I correct, and can somebody fix this, or am I using the library incorrectly?
Upvotes: 0
Views: 6371
Reputation: 23908
This may be dependent on when the test is run. I noticed you asked this near the end of a month. And February only has 28 days.
I suspect what happened is that you ran your code on the 29th, 30th, or 31st of the month, so the initial selection was something like 2012-11-30. When you set the month value to 01 (Feb), the day value stays 30, producing a "denormalized" date that rolls over in to the next month when the Calendar
tries to determine the actual date from the field values. Like "carrying" the overflow when you're doing decimal addition. This will give you a date like March 1. Then if you set the month field again to February, this time the day field is within the valid range for February, and you don't get overflow.
There is a pending Pull Request on the JDatePicker project to deal with this. In the mean time, you could use one of the methods that sets the entire date in a single call instead of setting the year, month, and day fields separately. Or set the day field before setting the month field to avoid the rollover condition.
Upvotes: 0
Reputation: 86
I noticed your package import is jat.jdatepicker.JDatePicker, which seems to be included directly into the source repo of https://sourceforge.net/p/jat
This version of JDatePicker you are using is a fork from the original JDatePicker project. Although forks are not disallowed for open source projects, it is advisable to try and commit back to the original project if there are issues which needs to be addressed.
I would advise you rather use the latest version of the project at https://github.com/JDatePicker/JDatePicker
I tested your case against jdatepicker-1.3.4 which can be downloaded from central repository (https://search.maven.org/#artifactdetails%7Corg.jdatepicker%7Cjdatepicker%7C1.3.4%7Cjar)
Include it with maven:
<dependency>
<groupId>org.jdatepicker</groupId>
<artifactId>jdatepicker</artifactId>
<version>1.3.4</version>
</dependency>
With the following test February was selected initially.
public static void main(String[] args) {
JFrame testFrame = new JFrame();
testFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
testFrame.setSize(500, 500);
JPanel jPanel = new JPanel();
JDatePicker picker = new JDateComponentFactory().createJDatePicker();
picker.setTextEditable(true);
picker.setShowYearButtons(true);
jPanel.add((JComponent) picker);
picker.getModel().setYear(2010);
picker.getModel().setMonth(1);
//picker.getModel().setMonth(1);
picker.getModel().setDay(15);
picker.getModel().setSelected(true);
JPanel datePanel = new JPanel();
datePanel.setLayout(new BorderLayout());
datePanel.add(jPanel, BorderLayout.WEST);
BorderLayout fb = new BorderLayout();
testFrame.setLayout(fb);
testFrame.getContentPane().add(datePanel, BorderLayout.WEST);
testFrame.setVisible(true);
}
Upvotes: 1