1、计算当前月总共的天数
public int getDays(int year, int month) {
int days = 0;
if (month != 2) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
}
} else {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
days = 29;
else
days = 28;
}
System.out.println("当月有" + days + "天!");
return days;
}
2、获取当前月开始与结束日期
public Map getBeginAndEndDate() {
Map map = new HashMap();
Calendar date = Calendar.getInstance();
String yearString = String.valueOf(date.get(Calendar.YEAR));
String monthString = String.valueOf(date.get(Calendar.MONTH) + 1);
String beginString = yearString + "-" + monthString + "-01";
int year = Integer.parseInt(yearString);
int month = Integer.parseInt(monthString);
int days = getDays(year, month);
String endString = yearString + "-" + monthString + "-" + days;
DateFormat df = new DateFormat();
Date benginDate = df.StringFormatToDate(beginString);
Date endDate = df.StringFormatToDate(endString);
map.put("benginDate", benginDate);
map.put("endDate", endDate);
return map;
};
|