The "best" solution is take advantage of the available functionality.
Java 8+ introduced the java.time
API, which replaces the Calendar
and Date
based APIs
For example, something like this, using YearMonth
class…
for (int year = 2010; year <= 2020; year++) {
YearMonth ym = YearMonth.of(year, Month.FEBRUARY);
System.out.println(year + " = " + ym.lengthOfMonth());
}
will print...
2010 = 28
2011 = 28
2012 = 29
2013 = 28
2014 = 28
2015 = 28
2016 = 29
2017 = 28
2018 = 28
2019 = 28
2020 = 29
From this, you can simply create a new ComboBoxModel
, fill it with the values you need and apply it to the instance of JComboBox
- see How to Use Combo Boxes for more details.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…