编写程序:将2008年的每个月的星期日打印出来。要求形如“2008年1月6日”

解决方案 »

  1.   


    import java.util.*;public class PrintSunday {
        public static void main(String[] args){
         //直接构造2008年的第一个星期日
         Calendar c = Calendar.getInstance();
         c.set(2008,1-1,6);
         //年份
         int year = c.get(Calendar.YEAR);
         //月份
         int month = c.get(Calendar.MONTH) + 1;
         //日期
         int day = c.get(Calendar.DATE);
         //循环打印后续的星期日,直到年份不是2008为止
         while(year == 2008){
         //打印日期
         System.out.println(year + "年" + month + "月" + day + "日");
         //计算下一个星期日
         c.add(Calendar.DATE,7);
         //获得年月日
         //年份
         year = c.get(Calendar.YEAR);
         //月份
         month = c.get(Calendar.MONTH) + 1;
         //日期
         day = c.get(Calendar.DATE);
         }
        }
        
    }
      

  2.   

    这个应该能实现了 仅供参考import java.util.*;public class GetSunday {
    public static void main(String[] args){
    Calendar now = Calendar.getInstance(); 
    now.set(2008,0,6);
    long time=now.getTime().getTime();
    System.out.println(time);
    for(int i=0;i<52;i++)
    {
    System.out.println(new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date(time)));
    time=time+604800000l;
    //System.out.println(time);
    }
    }
      

  3.   

    通用例子import java.util.*;public class Test{
    /**
     * 闰年判断
     * 
     * */
    public boolean isLeapYear(int year){
    if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
    return true;
    }
    else{
    return false;
    }
    }

    /**
     * year:年份
     * dayOfWeek: 星期  (0:周日,1:周一....)
     * 
     * */
    public void printSometing(int year,int dayOfWeek){
    Calendar calendar = Calendar.getInstance();
    int[] arrDay = {31,28,31,30,31,30,31,31,30,31,30,31};
    if (isLeapYear(year)){
    arrDay[1] = 29;
    }
    for(int i=0;i<arrDay.length;i++){ // 月份(注意是从0开始算的)
    for(int j=0;j<arrDay[i];j++){ // 日期
    calendar.set(year,(i),(j+1));
    if(calendar.get(Calendar.DAY_OF_WEEK) - 1 == dayOfWeek)
    System.out.println(year + "年" + (i + 1) + "月" + (j + 1) + "日");
    }
    }
    }

        public static void main(String[] args)  {
     //  2008,0 -- 2008年的周日
     //  2009,4 -- 2009年的周四
          new Test().printSometing(2009,4);
        }
    }
      

  4.   

    public GregorianCalendar(Locale aLocale)
    Constructs a GregorianCalendar based on the current time in the default time zone with the given locale. Parameters:
    aLocale - the given locale.--------------------------------------------------------------------------------GregorianCalendar
    public GregorianCalendar(TimeZone zone,
                             Locale aLocale)
    Constructs a GregorianCalendar based on the current time in the given time zone with the given locale. Parameters:
    zone - the given time zone.
    aLocale - the given locale.
      

  5.   

    这个不难吧
    好久没看过JAVA了
      

  6.   

    我在想个问题.. 我觉得3楼的方法不好...因为每次打印都要new一个对象.. 我觉得没必要..