我已將年,月,日分開放置JComboBoxes
月份JComboBox自己的renderer
可顯示月份名稱而不是月份的integer
值。在哪裏月開始於0 (0-January....11-December)
將JComboBox年,月,日整數值連接或格式化爲日期類型
因此,我的問題是如何將年,月,日放在一起並將其分配給Date
數據類型。
我試圖串聯年,月,日一起到String
可變
int startYear = Integer.parseInt(jcmbFirstSemStartDateYear.getSelectedItem().toString());
int startMonth = Integer.parseInt(jcmbFirstSemStartDateMonth.getSelectedItem().toString());
int startDay = Integer.parseInt(jcmbFirstSemStartDateDay.getSelectedItem().toString());
String startDate = startYear+"-"+startMonth+"-"+startDay; //not sure if this okay
Date completeStartDate = java.sql.Date.valueOf(startDate);
JOptionPane.showMessageDialog(null,completeStartDate);
hasConflict(completeStartDate, completeEndDate); //method to check conflict
但是當我打印的completeStartDate
值時,它顯示二月份爲01
我擔心這可能會被讀爲1月份,而不是2月份,當我提取月份來檢查衝突。
public static boolean hasConflict(Date aStartDate, Date aEndDate){
boolean hasConflict = false;
Calendar calStart = Calendar.getInstance();
calStart.setTime(aStartDate);
int startYear = calStart.get(Calendar.YEAR);
int startMonth = calStart.get(Calendar.MONTH); //extract month
int startDay = calStart.get(Calendar.DAY_OF_MONTH);
// ... if else conditions to compare startDate with endDate
return hasConflict;
}
後來,我將這些日期存儲到mysql
數據庫。
任何想法或建議?
謝謝。
你可以創建一個String表示然後根據格式解析它,或者你可以使用['LocalDate.of(year,month,dayOfMonth)'](https://docs.oracle.com/javase /8/docs/api/java/time/LocalDate.html#of-int-int-int-) – MadProgrammer