0
根據問題的條款,需要在每個月以及用戶在組合框中使用名稱和數字在組合框中選擇的一週中的星期幾(星期幾除外)一年中的月份。我發現一個解決方案:在java中使用日曆
public static List<String> getWeekDays(int year, int month)
{
List<String> weekDays = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, 1);
int max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
while (--max >= 0)
{
switch (calendar.get(Calendar.DAY_OF_WEEK))
{
case Calendar.MONDAY:
weekDays.add("Пн#"+ Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
break;
case Calendar.TUESDAY:
weekDays.add("Вт#"+ Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
break;
case Calendar.WEDNESDAY:
weekDays.add("Ср#"+ Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
break;
case Calendar.THURSDAY:
weekDays.add("Чт#"+ Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
break;
case Calendar.FRIDAY:
weekDays.add("Пт#"+Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
break;
case Calendar.SATURDAY:
weekDays.add("Сб#"+ Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
break;
}
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
return weekDays;
}
但它只顯示當前月份。試圖每月更改它calendar.add(Calendar.MONTH,1);但顯示廢話,在2015年11月爲31天,並在本週二開始。
calendar.set(Calendar.YEAR, year);
System.out.println(" Date with month changed : " + sdfY.format(calendar.getTime()));
calendar.set(Calendar.MONTH, month-1);
System.out.println(" Date with month changed : " + sdfM.format(calendar.getTime()));
System.out.println(calendar.getFirstDayOfWeek());
calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
在這片顯示在十一月每月正確的數據,2015年,周2號的第一天,但由於某些原因的週期開始從週一30日來算。
for(int i = 0; i <= calendar.getActualMaximum(Calendar.DAY_OF_MONTH); i++)
{
switch (calendar.get(Calendar.DAY_OF_WEEK))
{
case Calendar.MONDAY:
weekDays.add("Пн#"
+ Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
break;
case Calendar.TUESDAY:
weekDays.add("Вт#"
+ Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
break;
case Calendar.WEDNESDAY:
weekDays.add("Ср#"
+ Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
break;
case Calendar.THURSDAY:
weekDays.add("Чт#"
+ Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
break;
case Calendar.FRIDAY:
weekDays.add("Пт#"
+ Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
break;
case Calendar.SATURDAY:
weekDays.add("Сб#"
+ Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
break;
}
calendar.add(Calendar.DAY_OF_MONTH, 1);
}