java根据日期获取当前是星期几?

/**
  * @author jerry.chen
  * @param dateStr
  * @return 获取当前是星期几
  */
 public static String getCurrentWeekOfMonth(String dateStr) {
  String weekStr = "";
  int year = 0, month = 0, day = 0;
  try {
   String[] year_month_day = dateStr.split("-");
   if (!"".equals(year_month_day[0])) {
    year = Integer.parseInt(year_month_day[0]);
   }
   if (!"".equals(year_month_day[1])) {
    month = Integer.parseInt(year_month_day[1]);
   }
   if (!"".equals(year_month_day[2])) {
    day = Integer.parseInt(year_month_day[2]);
   }
   Calendar calendar = new GregorianCalendar(year, month - 1, day);
   if ("星期天".equals(getCurrentWeekOfMonth(calendar))) {
    weekStr = "1";
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return weekStr;
 }

 /**
  * @author jerry.chen
  * @param calendar
  * @return 获取当前是星期几;
  */
 public static String getCurrentWeekOfMonth(Calendar calendar) {
  String strWeek = "";
  int dw = calendar.get(Calendar.DAY_OF_WEEK);
  if (dw == 1) {
   strWeek = "星期天";
  } else if (dw == 2) {
   strWeek = "星期一";
  } else if (dw == 3) {
   strWeek = "星期二";
  } else if (dw == 4) {
   strWeek = "星期三";
  } else if (dw == 5) {
   strWeek = "星期四";
  } else if (dw == 6) {
   strWeek = "星期五";
  } else if (dw == 7) {
   strWeek = "星期六";
  }
  return strWeek;
 }

 /**
  * @author jerry.chen
  * @param calendar
  * @return 返回当前是星期几;
  */
 public static int getCurrentWeekOfMonthIndex(Calendar calendar) {
  // System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
  return calendar.get(Calendar.DAY_OF_WEEK);
 }

 

posted on 2013-06-15 10:42  大山下的蚂蚁  阅读(4059)  评论(0编辑  收藏  举报

导航