获取本周或月的某一天

/**
  * 得到本周周一
  * 
  * @return yyyy-MM-dd
  */
 public static String getMondayOfThisWeek() {
  Calendar c = Calendar.getInstance();
  int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
  if (dayofweek == 0)
   dayofweek = 7;
  c.add(Calendar.DATE, -dayofweek + 1);
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  return sdf.format(c.getTime());
 }

/**
  *得到本周周日 
  * @return yyyy-MM-dd
  */
 public static String getSundayOfThisWeek() {
  Calendar c = Calendar.getInstance();
  int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
  if (dayofweek == 0)
   dayofweek = 7;
  c.add(Calendar.DATE, -dayofweek + 7);
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  return sdf.format(c.getTime());
 }

/**
  * 得到本月最后一天
  * 
  * @return
  */
 public static String getLastDateOfMonth()
 {
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  Date dt = getDate();

  if (dt == null)
   return null;
  Calendar cal = Calendar.getInstance();
  cal.setTime(dt);
  int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
  cal.set(Calendar.DAY_OF_MONTH, days);
  String result = format.format(cal.getTime());
  System.out.println("一个月最后一天" + result);
  return result;
 }

 /**
  * 得到本月第一天
  * 
  * @return
  */
 public static String getFristDateOfMonth()
 {
  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  Date dt = getDate();

  if (dt == null)
   return null;
  Calendar cal = Calendar.getInstance();
  cal.setTime(dt);
  int days = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
  cal.set(Calendar.DAY_OF_MONTH, days);
  String result = format.format(cal.getTime());
  System.out.println("一个月第一天" + result);
  return result;
 }


posted @ 2010-01-14 18:12  QQ天堂  阅读(296)  评论(0)    收藏  举报