题目:要求编程输出2008年日历.日历中要求有月份,日期与星期(如星期一和星期二等).然后统计并输出日期的个位数与相应的星期恰好相同的天数(例如:2008年9月1日恰好是星期一).....满分啊!

解决方案 »

  1.   


    package example;import java.util.*;public class test {
      public static void main(String[] args) {
        Calendar date = Calendar.getInstance();
        for (int i = 1; i <= 366; i++) {
          date.set(2008, 0, i); //设置日期为1月1号,每次加1,2008年有366天
          int day = date.get(Calendar.DAY_OF_MONTH); //返回当前时间月中的某天;
          int week = date.get( (Calendar.DAY_OF_WEEK)) - 1; //返回当前时间星期几,西方星期重星期天开始,所以-1;
          //如果星期为0,就是星期天
          if (week == 0) {
            week = 7;
          }
          //满足条件
          if (day == week) {
            System.out.println(new test().getDate(date, week));
          }
        }  }
      /**
       * 格式日期
       * @param date Calendar
       * @param week int
       * @return String
       */
      public String getDate(Calendar date, int week) {
        String str = date.get(Calendar.YEAR) + "年" + (date.get(Calendar.MONTH) + 1) +
            "月" +
            date.get(Calendar.DATE) + "日  星期" + week;
        return str;
      }
    }