1.使用calendar类实现
2.使用自己编写的函数实现
package test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
publicclass DateUtils
{
      //返回当前年月日
      String getNowDate()
      {
            Date date = new Date();
            String nowDate = new SimpleDateFormat(
"yyyy年MM月dd日"
).format(date);
            return nowDate;
      }
      //返回当前年份
      int getYear()
      {
            Date date = new Date();
            String year = new SimpleDateFormat("yyyy").format(date);
            return Integer.parseInt(year);
      }
      //返回当前月份
      int getMonth()
      {
            Date date = new Date();
            String month = new SimpleDateFormat("MM").format(date);
            return Integer.parseInt(month);
      }
      //判断闰年
      boolean isLeap(int year)
      {
            if (((year % 100 == 0) && year % 400 == 0) ((year % 100 != 0) && year % 4 == 0))
            returntrue;
            else
            returnfalse;
      }
      //返回当月天数
      int getDays(int year, int month)
      {
            int days;
            int FebDay = 28;
            if (isLeap(year))
            FebDay = 29;
            switch (month)
            {
                  case1:
                  case3:
                  case5:
                  case7:
                  case8:
                  case10:
                  case12:
                  days = 31;
                  break;
                  case4:
                  case6:
                  case9:
                  case11:
                  days = 30;
                  break;
                  case2:
                  days = FebDay;
                  break;
                  default:
                  days = 0;
                  break;
            }
            return days;
      }
      //返回当月星期天数
      int getSundays(int year, int month)
      {
            int sundays = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
            Calendar setDate = Calendar.getInstance();
            //从第一天开始
            int day;
            for (day = 1; day <= getDays(year, month); day++)
            {
                  setDate.set(Calendar.DATE, day);
                  String str = sdf.format(setDate.getTime());
                  if (str.equals(
"星期日"
))
                  {
                        sundays++;
                  }
            }
            return sundays;
      }
      publicstaticvoid main(String[] args)
      {
            DateUtils du = new DateUtils();
            System.out.println("今天日期是:" + du.getNowDate());
            System.out.println("本月有" + du.getDays(du.getYear(), du.getMonth()) + "天");
            System.out.println("本月有" + du.getSundays(du.getYear(), du.getMonth()) + "个星期天");
      }
}--转自 

该贴由hui.chen转至本版2015-7-30 15:23:14