在Java中如何输入任何一个年份的,打印出全年的星期日  例如: 输入2012  打印出2012年所以的星期天的日期

解决方案 »

  1.   

    public void getAllSunday(int year){
    Calendar c = Calendar.getInstance();
    c.set(year,0,1);//设置传入年的1月1号

    int first = c.get(Calendar.DAY_OF_WEEK);
    for(int i = 0;i < 7;i++){//找到全年第一个星期日
    if(first == 1){
    System.out.println(c.get(Calendar.MONDAY) + 1 + "月" + c.get(Calendar.DAY_OF_MONTH) + "日");
    c.add(Calendar.DAY_OF_MONTH,7);
    break;
    }else{
    c.add(Calendar.DAY_OF_MONTH,1);
    }
    }

    Calendar end = Calendar.getInstance();  
    end.set(year + 1, 0, 1);
    while(c.before(end)){//循环,加7,直到年底
    System.out.println(c.get(Calendar.MONDAY) + 1 + "月" + c.get(Calendar.DAY_OF_MONTH) + "日");
    c.add(Calendar.DAY_OF_YEAR, 7);
    }
    }
    找到全年第一个星期日,然后就一直加7吧,打印对应的日期
      

  2.   

    public void getAllSunday(int year){
    Calendar c = Calendar.getInstance();
    c.set(year,0,1);//设置传入年的1月1号

    int first = c.get(Calendar.DAY_OF_WEEK);
    for(int i = 0;i < 7;i++){//找到全年第一个星期日
    if(first == 1){
    System.out.println(c.get(Calendar.MONDAY) + 1 + "月" + c.get(Calendar.DAY_OF_MONTH) + "日");
    c.add(Calendar.DAY_OF_MONTH,7);
    break;
    }else{
    c.add(Calendar.DAY_OF_MONTH,1);
    }
    }

    Calendar end = Calendar.getInstance();  
    end.set(year + 1, 0, 1);
    while(c.before(end)){//循环,加7,直到年底
    System.out.println(c.get(Calendar.MONDAY) + 1 + "月" + c.get(Calendar.DAY_OF_MONTH) + "日");
    c.add(Calendar.DAY_OF_YEAR, 7);
    }
    }
    找到全年第一个星期日,然后就一直加7吧,打印对应的日期