public class CalenderUtil { /** * 取得当月天数 */ public static int getCurrentMonthLastDay() { Calendar a = Calendar.getInstance(); a.set(Calendar.DATE, 1);//把日期设置为当月第一天 a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天 int maxDate = a.get(Calendar.DATE); return maxDate; } /** * 得到指定月的天数 */ public static int getMonthLastDay(int year, int month) { Calendar a = Calendar.getInstance(); a.set(Calendar.YEAR, year); a.set(Calendar.MONTH, month - 1); a.set(Calendar.DATE, 1);//把日期设置为当月第一天 a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天 int maxDate = a.get(Calendar.DATE); return maxDate; } //返回当前年月日 public static String getNowDate() { Date date = new Date(); String nowDate = new SimpleDateFormat("yyyy年MM月dd日").format(date); return nowDate; } //返回当前年份 public static int getYear() { Date date = new Date(); String year = new SimpleDateFormat("yyyy").format(date); return Integer.parseInt(year); } //返回当前月份 public static int getMonth() { Date date = new Date(); String month = new SimpleDateFormat("MM").format(date); return Integer.parseInt(month); } //判断闰年 public static boolean isLeap(int year) { if (((year % 100 == 0) && year % 400 == 0) || ((year % 100 != 0) && year % 4 == 0)) return true; else return false; } //返回当月天数 public static int getDays(int year, int month) { int days; int FebDay = 28; if (isLeap(year)) FebDay = 29; 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; break; case 2: days = FebDay; break; default: days = 0; break; } return days; } /** * * @param year * @param month */ public static List<TimeBean> getDaysInMonth(int year, int month) { List<TimeBean> timeBeans=new ArrayList<>(); Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month-1);//注意一定要写5,不要写6!Calendar.MONTH是从0到11的! int n = c.getActualMaximum(Calendar.DAY_OF_MONTH); // Log.e("日历", year+"年"+month+"月有" + n + "天" ); for (int i = 0; i < n; i++) { c.set(Calendar.DAY_OF_MONTH,i+1); int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); // Log.e("日历",year+"年"+month+"月"+(i+1)+"号,星期 "+(dayOfWeek-1)); timeBeans.add(new TimeBean(0,year,month,i+1,(dayOfWeek-1))); } return timeBeans; } }
TimeBean:
public class TimeBean { private int type;//颜色控制 private int year; private int month; private int day; private int dayInWeek;
}