各位大虾,如何用java 算一年中所有周末的日期?
比如我传入一个参数"2005"
就可以输出2005年中所有周末的日期....

解决方案 »

  1.   

    使用calendar类,就可以达到这个目的
      

  2.   

    public static void getWeek(int year){

    ArrayList al = new ArrayList();
    Calendar c = Calendar.getInstance();
    c.setTime(new Date(year-1900,0,1));

    while(c.getTime().getDay() !=0){
    c.add(Calendar.DAY_OF_WEEK,1);
    }
    al.add(c.getTime());
    while(c.getTime().getYear() == year-1900){
    System.out.println(al);
    c.add(Calendar.DATE,7);
    al.add(c.getTime());
    }
    System.out.println("al:"+al.size());
    }
      

  3.   

    先算出元旦星期,进而得到第一个周末是几号
    每次将日期加7,超出当月天数,则减去一个月的天数计算元旦星期的公式:(y + (y-1)/4 - (y-1)/100 + (y-1) / 400) % 7
    可以参考:http://community.csdn.net/Expert/topic/3966/3966336.xml?temp=.5583155public class SunList {    public static void main(String[] args) {
            int y = 2005;
            int w = 5; // 哪天算周末?6-0,0表示星期日
            
            int m=1, d;        int e[] = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
            boolean leap = y%4==0 && y%100!=0 || y%400==0;
            e[1] = leap ? 29 : 28;
            
            // 元旦星期几?
            d = (y + ((y-1) >> 2) - (y-1)/100 + (y-1) / 400) % 7;        // 第一个周末几号
            if(d == w) {
                d = 1;
            }
            else {
                d = (w>d ? w-d : (w-d+7) ) + 1;
            }        // 是否有第53个周末
            boolean add = (d==1 || d<=2 && leap);
            
            // 连续52个周末     
            for(int i=0; i<52; ++i) {
                System.out.println(i+1 + " : " + y + "-" + m + "-" + d);
                
                d += 7;
                if(d > e[m-1] ) {
                    d -= e[m-1];
                    ++m;
                }
            }
            
            if(add) {
                System.out.println(53 + " : " + y + "-" + 12 + "-" + d);
            }
        }
    }
      

  4.   

    正好不久前写了一个:
      /**
       * 查询一个月的第一个星期天
       * @param year int
       * @param month int
       * @return int 1-31的一天
       */
      public static int getFirstSundayOfWeek(int year, int month){
          int i;
          for(i = 1; i <= 7; i++){
              if(getDayOfWeek(year, month, i) == 1){
                  break;
              }
          }
          return i;
      }
      

  5.   

    还有
     /**
       * 计算星期几的函数
       * @param year int
       * @param month int
       * @param date int
       * @return int 1~7代表星期日到星期六
       */
      public static int getDayOfWeek(int year, int month, int date){
          Calendar cal = Calendar.getInstance();
          cal.setTimeZone(TimeZone.getDefault());
          cal.clear();
          cal.set(year, month - 1, date);
          return cal.get(cal.DAY_OF_WEEK);
      }