给定年份和月份输出当月日历
笔试:算法题————获取输入三个数,获取本月的日历
//定义全局遍历list ArrayList<ArrayList<String>> resList= new ArrayList(); ArrayList<String> subList= new ArrayList(); public ArrayList<ArrayList<String>> calendar(int year,int month,int dayofWeek){ Calendar cal=Calendar.getInstance(); //设置cal cal.set(year,month-1,1)//由于Java中是从0开始计数,所以要减一 //获取本月第一天是周几 int startDay=cal.get(Calendar.DAY_OF_WEEK)-1;//也需要进行减一 int maxDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH); for(int i=1;i<startDay;i++){ //打印空格 subList.add("\"\""); } //开始打印日期 for(int i=1;i<=maxDay;i++){ subList.add(i+""); if(subList.size()>=7){ resList.add(subList); subList=new ArrayList(); } } //判断subList中是否为空,不为空,则还有数据 if(subList.size()>0){ for(int i=0;i<7-subList();i++){ subList.add("\"\""); } } resList.add(subList); return resList; }