日期:2014-05-20 浏览次数:20930 次
private String sunday; private String monday; private String tuesday; private String wednesday; private String thursday; private String friday; private String saturday;
Calendar c = Calendar.getInstance(); c.set(Calendar.Date, 1); //获得1号是星期几 int start = c.get(Calendar.DAY_OF_WEEK); //获得当前月的最大日期数 int maxDay = c.getActualMaximum(Calendar.DATE); System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六"); for(int i = 1; i < start; i++){ System.out.print("\t"); } for(int i = 1; i <= maxDay; i++){ System.out.print("" + i); System.out.print("\t"); if(i < 10){ System.out.print(""); } if((start + i - 1)%7 == 0){ System.out.println(); } System.out.println(); }
------解决方案--------------------
看楼主是做android的吧 所以很多时候不能用jdk里面的方法 因为不知道android里面的jdk有没有那个类或者其中的方法 所以最好自己写了
public int getWeekDay(int year,int month){ int days=1;//这里以windows操作系统为例 也就是:日一二三四五六 如果要换其他的可以在这个方法里面改 for(int i=1900;i<year;i++){ if(isLeapYear(i)){ days+=366; } else{ days+=365; } } for (int i = 1; i < month; i++) { days+=getMonthDay(year, i); } return days%7; } public boolean isLeapYear(int year){ return ((year%4==0&&year%100!=0)||year%400==0); } public int getMonthDay(int year,int month){ //31:1 3 5 7 8 10 12 30:4 6 9 11 28/29:2 if(month==2){ if(isLeapYear(year)){ return 29; } else{ return 28; } } else if(month<13){ if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){ return 31; } else{ return 30; } } return 0; } public int[][] getMonthData(int year,int month){ int weekDay=getWeekDay(year, month);//获取本月第一天是星期几 int ed=getMonthDay(year, month)-(7-weekDay); int length=1; int point=ed%7; if(point>0){ length+=ed/7+1; } else{ length+=ed/7; } int[][] data=new int[length][7]; int count=0; int week=0; for (int i = 1; i <= getMonthDay(year, month); i++) { if(count==0){ if(i==1){ week+=weekDay; } data[count][week]=i; } else{ data[count][week]=i; } week++; if((weekDay+i)%7==0){ count++; } if(week==7){ week=0; } } return data; } public String getString(int num){ return num==0?"":num+""; } public ArrayList<DateTimeV> getMonth(int year,int month){ int[][] data=getMonthData(year, month); ArrayList<DateTimeV> list=new ArrayList<DateTimeV>(); for (int i = 0; i < data.length; i++) { list.add(new DateTimeV(getString(data[i][0]), getString(data[i][0]), getString(data[i][0]), getString(data[i][0]), getString(data[i][0]), getString(data[i][0]), getString(data[i][0]))); } return list; }