Reputation: 2098
I have a 3D array that contains 38 years, 12 months, and 31 entries for each month (regardless of how many days in that month). Like so: array[38][12][31]
. I also have a JCalendar that is doing nothing now except looking pretty, and the JCalendar has a button underneath. How would I make it so that I can select a date in the calendar, then press the button and it returns the element of my array that would correspond to that date?
Something like
if(buttonPressed){
year = chosenYear - 1975;
month = chosenMonth;
day = chosenDay;
System.out.print(array[year][month][day]);
}
thanks guys.
Upvotes: 2
Views: 526
Reputation: 205865
You can get the selected Date
in a PropertyChangeListener
, as shown here. Once you have the date
, you can get the year, month and day from a Calendar
:
Calendar c = Calendar.getInstance();
c.setTime(date);
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH);
int d = c.get(Calendar.DAY_OF_MONTH);
Calendar.MONTH
is already zero-based, but Calendar.DAY_OF_MONTH
is not; and you'll need to adjust the year to your baseline.
Upvotes: 2