android - how to show previousmonth dates in calender -
this ccalender code below work fine problem is not display date before first day of month see http://imgur.com/inl1gze , after lastday of month full source code here https://www.zeta-uploader.com/1699876023
want sun mon tues wed thur fri sat 29 30 31 1 2 3 4 public class hoyahcalendar extends activity { public static int myear; public static int currentindex = -1; public static int mmonth; public static int mday; public static string[][] = new string[6][7]; calendar mcalendar = calendar.getinstance(); myear = mcalendar.get(calendar.year); mmonth = mcalendar.get(calendar.month) + 1; mday = mcalendar.get(calendar.day_of_month); last_week.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (mmonth == 1) { myear -= 1; mmonth = 12; new showcalendar(myear, mmonth, mday, "last"); showonscreen(); } else { // mmonth -= 1; new showcalendar(myear, mmonth, mday, "last"); showonscreen(); } } }); next_week.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { if (mmonth == 12) { myear += 1; mmonth = 1; new showcalendar(myear, mmonth, mday, "next"); showonscreen(); } else { if (hoyahcalendar.currentindex == 4) { hoyahcalendar.currentindex = 4; // mmonth += 1; } new showcalendar(myear, mmonth, mday, "next"); showonscreen(); } } }); new showcalendar(myear, mmonth); showonscreen(); public class showcalendar { int myear; int mmonth; int mday; public showcalendar(int myear, int mmonth){ this.myear = myear; this.mmonth = mmonth; calculatemonthfirstday(); } public showcalendar(int myear, int mmonth, int mday){ this.myear = myear; this.mmonth = mmonth; hoyahcalendar.currentindex = 0; this.mday = mday; calculatemonthfirstday(); } public int getmday() { return mday; } public void setmday(int mday) { this.mday = mday; } public showcalendar(int myear, int mmonth, int mday, string time){ this.myear = myear; this.mmonth = mmonth; if (time == "next"){ hoyahcalendar.currentindex++; if (hoyahcalendar.currentindex == 5){ hoyahcalendar.currentindex--; } this.mday = mday + 7; } else if (time == "last"){ hoyahcalendar.currentindex--; if (hoyahcalendar.currentindex == -1){ hoyahcalendar.currentindex++; } this.mday = mday - 7; } calculatemonthfirstday(); } public void calculatemonthfirstday(){ int month, first_day=0; if((myear%4==0 && myear%100!=0)||(myear%400==0)) month=1; else month=0; int y, y12, c, c12, m, d; y = myear%100; y12 = (myear-1)%100; //only january , february c = myear/100; c12 = (myear-1)/100; m = mmonth; d = 1; switch(mmonth){ case 1: {first_day = y12 + y12/4 +c12/4 - 2*c12 + 26*(13 + 1)/10 + d - 1;break;} case 2: {first_day = y12 + y12/4 +c12/4 - 2*c12 + 26*(14 + 1)/10 + d - 1;break;} case 3: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;} case 4: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;} case 5: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;} case 6: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;} case 7: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;} case 8: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;} case 9: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;} case 10: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;} case 11: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;} case 12: {first_day = y + y/4 +c/4 - 2*c + 26*(m + 1)/10 + d - 1;break;} } if(first_day<0) first_day = 7 - (math.abs(first_day))%7;//first_day每月第一天星期几 else first_day = first_day%7; switch(mmonth){ case 1: {calculatecalendar(1,first_day,31);break;} case 2: {calculatecalendar(2,first_day,28+month);break;} case 3: {calculatecalendar(3,first_day,31);break;} case 4: {calculatecalendar(4,first_day,30);break;} case 5: {calculatecalendar(5,first_day,31);break;} case 6: {calculatecalendar(6,first_day,30);break;} case 7: {calculatecalendar(7,first_day,31);break;} case 8: {calculatecalendar(8,first_day,31);break;} case 9: {calculatecalendar(9,first_day,30);break;} case 10:{calculatecalendar(10,first_day,31);break;} case 11:{calculatecalendar(11,first_day,30);break;} case 12:{calculatecalendar(12,first_day,31);break;} } } public void calculatecalendar(int month_no, int week_no, int month_days){ int i, s = 0; int currentday; if (this.mday == 0){ mday = 1; currentday= hoyahcalendar.mday; }else { currentday = this.mday; } //string[][] = new string[6][7]; (i=0;i<week_no;i++) hoyahcalendar.a[i/7][i%7] = ""; for(i=week_no; i<week_no + month_days; i++){ s = - week_no + 1; hoyahcalendar.a[i/7][i%7] = string.valueof(s); if (s == currentday && hoyahcalendar.currentindex == -1){ hoyahcalendar.currentindex = i/7; } } (i=0; i<7;i++){ if (hoyahcalendar.a[hoyahcalendar.currentindex][i] == null){ hoyahcalendar.a[0][i] = ""; }else{ hoyahcalendar.a[0][i] = hoyahcalendar.a[hoyahcalendar.currentindex][i]; } } for(i=week_no+month_days; i<42; i++) hoyahcalendar.a[i/7][i%7] = ""; } }
here solution problem. first need different design of function calculate month calendar array of 6x7 use function takes year , month parameters, generate accurate array of month calendar.
/** * * @param year year * @param month january=0, feb = 1, ...., nov = 10, , dec = 11, know mean * @return */ private static int[][] getarraycalendar(int year,int month) { //very important //sun = 1, mon = 2, tu = 3, ..., fri = 6, sat = 7 according usa int[][] = new int[6][7]; int day = 1; //day must 1 calendar cal = calendar.getinstance(); cal.set(calendar.year, year); cal.set(calendar.month, month); cal.set(calendar.day_of_month, day); int dayofweek = cal.get(calendar.day_of_week); int goback = 1 - dayofweek; cal.add(calendar.day_of_month, goback); system.out.println("su\tmo\ttu\twe\tth\tfr\tsa"); for(int i=0;i<a.length;i++) { for(int j=0;j<a[0].length;j++) { a[i][j] = cal.get(calendar.day_of_month); system.out.print(cal.get(calendar.day_of_month)); system.out.print("\t"); cal.add(calendar.day_of_month, 1); } system.out.println(); } return a; }
example: getarraycalendar(2013,4)// may=4, year= 2013 result printed:
su mo tu th fr sa 28 29 30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1 2 3 4 5 6 7 8
- quick fix issue: modify showcalendar class following:
package com.example.calender5example; import java.util.calendar; public class showcalendar { int myear; int mmonth; int mday; public showcalendar(int myear, int mmonth) { this.myear = myear; this.mmonth = mmonth; calculatemonthfirstday(); } public showcalendar(int myear, int mmonth, int mday) { this.myear = myear; this.mmonth = mmonth; hoyahcalendar.currentindex = 0; this.mday = mday; calculatemonthfirstday(); } public int getmday() { return mday; } public void setmday(int mday) { this.mday = mday; } public showcalendar(int myear, int mmonth, int mday, string time) { this.myear = myear; this.mmonth = mmonth; if (time == "next") { hoyahcalendar.currentindex++; if (hoyahcalendar.currentindex == 5) { hoyahcalendar.currentindex--; } this.mday = mday + 7; } else if (time == "last") { hoyahcalendar.currentindex--; if (hoyahcalendar.currentindex == -1) { hoyahcalendar.currentindex++; } this.mday = mday - 7; } calculatemonthfirstday(); } public void calculatemonthfirstday() { int month, first_day = 0; if ((myear % 4 == 0 && myear % 100 != 0) || (myear % 400 == 0)) month = 1; else month = 0; int y, y12, c, c12, m, d; y = myear % 100; y12 = (myear - 1) % 100; // january , february c = myear / 100; c12 = (myear - 1) / 100; m = mmonth; d = 1; switch (mmonth) { case 1: { first_day = y12 + y12 / 4 + c12 / 4 - 2 * c12 + 26 * (13 + 1) / 10 + d - 1; break; } case 2: { first_day = y12 + y12 / 4 + c12 / 4 - 2 * c12 + 26 * (14 + 1) / 10 + d - 1; break; } case 3: { first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1; break; } case 4: { first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1; break; } case 5: { first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1; break; } case 6: { first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1; break; } case 7: { first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1; break; } case 8: { first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1; break; } case 9: { first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1; break; } case 10: { first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1; break; } case 11: { first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1; break; } case 12: { first_day = y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1; break; } } if (first_day < 0) first_day = 7 - (math.abs(first_day)) % 7;// first_day每月第一天星期几 else first_day = first_day % 7; switch (mmonth) { case 1: { calculatecalendar(1, first_day, 31); break; } case 2: { calculatecalendar(2, first_day, 28 + month); break; } case 3: { calculatecalendar(3, first_day, 31); break; } case 4: { calculatecalendar(4, first_day, 30); break; } case 5: { calculatecalendar(5, first_day, 31); break; } case 6: { calculatecalendar(6, first_day, 30); break; } case 7: { calculatecalendar(7, first_day, 31); break; } case 8: { calculatecalendar(8, first_day, 31); break; } case 9: { calculatecalendar(9, first_day, 30); break; } case 10: { calculatecalendar(10, first_day, 31); break; } case 11: { calculatecalendar(11, first_day, 30); break; } case 12: { calculatecalendar(12, first_day, 31); break; } } } public void calculatecalendar(int month_no, int week_no, int month_days) { int i, s = 0; int currentday; if (this.mday == 0) { mday = 1; currentday = hoyahcalendar.mday; } else { currentday = this.mday; } // string[][] = new string[6][7]; (i = 0; < week_no; i++) hoyahcalendar.a[i / 7][i % 7] = ""; (i = week_no; < week_no + month_days; i++) { s = - week_no + 1; hoyahcalendar.a[i / 7][i % 7] = string.valueof(s); if (s == currentday && hoyahcalendar.currentindex == -1) { hoyahcalendar.currentindex = / 7; } } (i = 0; < 7; i++) { if (hoyahcalendar.a[hoyahcalendar.currentindex][i] == null) { hoyahcalendar.a[0][i] = ""; } else { hoyahcalendar.a[0][i] = hoyahcalendar.a[hoyahcalendar.currentindex][i]; } } (i = week_no + month_days; < 42; i++) hoyahcalendar.a[i / 7][i % 7] = ""; //my fix starts here, don't want miss code int a[][] = getarraycalendar(myear, mmonth); hoyahcalendar.a = tostringarray(a);// here replaced array } private string[][] tostringarray(int[][] a) { string[][] array = new string[6][7]; (int = 0; < a.length; i++) { (int j = 0; j < a[0].length; j++) { array[i][j] = integer.tostring(a[i][j]); } system.out.println(); } return array; } /** * * @param year * year * @param month * january=0, feb = 1, ...., nov = 10, , dec = 11, know * mean * @return */ private static int[][] getarraycalendar(int year, int month) { // important // sun = 1, mon = 2, tu = 3, ..., fri = 6, sat = 7 according usa int[][] = new int[6][7]; int day = 1; // day must 1 calendar cal = calendar.getinstance(); cal.set(calendar.year, year); cal.set(calendar.month, month); cal.set(calendar.day_of_month, day); int dayofweek = cal.get(calendar.day_of_week); int goback = 1 - dayofweek; cal.add(calendar.day_of_month, goback); system.out.println("su\tmo\ttu\twe\tth\tfr\tsa"); (int = 0; < a.length; i++) { (int j = 0; j < a[0].length; j++) { a[i][j] = cal.get(calendar.day_of_month); system.out.print(cal.get(calendar.day_of_month)); system.out.print("\t"); cal.add(calendar.day_of_month, 1); } system.out.println(); } return a; } }
now can use function generate full monthely calendar (year,month) , using navigate through ui.
Comments
Post a Comment