答案 0 :(得分:0)
答案 1 :(得分:0)
我首先找到制作自定义日历的方法,我从日历中获取当天日期,然后执行以下计算
private void printMonth(int mm, int yy) {
Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
int trailingSpaces = 0;
int daysInPrevMonth = 0;
int prevMonth = 0;
int prevYear = 0;
int nextMonth = 0;
int nextYear = 0;
int currentMonth = mm - 1;
String currentMonthName = getMonthAsString(currentMonth);
daysInMonth = getNumberOfDaysOfMonth(currentMonth);
Log.d(tag, "Current Month: " + " " + currentMonthName + " having "
+ daysInMonth + " days.");
GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());
if (currentMonth == 11) {
prevMonth = currentMonth - 1;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 0;
prevYear = yy;
nextYear = yy + 1;
Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:"
+ prevMonth + " NextMonth: " + nextMonth
+ " NextYear: " + nextYear);
} else if (currentMonth == 0) {
prevMonth = 11;
prevYear = yy - 1;
nextYear = yy;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 1;
Log.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:"
+ prevMonth + " NextMonth: " + nextMonth
+ " NextYear: " + nextYear);
} else {
prevMonth = currentMonth - 1;
nextMonth = currentMonth + 1;
nextYear = yy;
prevYear = yy;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
Log.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:"
+ prevMonth + " NextMonth: " + nextMonth
+ " NextYear: " + nextYear);
}
int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
trailingSpaces = currentWeekDay;
Log.d(tag, "Week Day:" + currentWeekDay + " is "
+ getWeekDayAsString(currentWeekDay));
Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);
if (cal.isLeapYear(cal.get(Calendar.YEAR)))
if (mm == 2)
++daysInMonth;
else if (mm == 3)
++daysInPrevMonth;
// Trailing Month days
for (int i = 0; i < trailingSpaces; i++) {
Log.d(tag,
"PREV MONTH:= "
+ prevMonth
+ " => "
+ getMonthAsString(prevMonth)
+ " "
+ String.valueOf((daysInPrevMonth
- trailingSpaces + DAY_OFFSET)
+ i));
list.add(String
.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET)
+ i)
+ "-GREY"
+ "-"
+ getMonthAsString(prevMonth)
+ "-"
+ prevYear);
}
// Current Month Days
for (int i = 1; i <= daysInMonth; i++) {
Log.d(currentMonthName, String.valueOf(i) + " "
+ getMonthAsString(currentMonth) + " " + yy);
if (i == getCurrentDayOfMonth()) {
list.add(String.valueOf(i) + "-BLUE" + "-"
+ getMonthAsString(currentMonth) + "-" + yy);
} else {
list.add(String.valueOf(i) + "-WHITE" + "-"
+ getMonthAsString(currentMonth) + "-" + yy);
}
}
// Leading Month days
for (int i = 0; i < list.size() % 7; i++) {
Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));
list.add(String.valueOf(i + 1) + "-GREY" + "-"
+ getMonthAsString(nextMonth) + "-" + nextYear);
}