08-12
09

Calendar获取本月或本周日期

1.在计算自然周时  cal.roll(Calendar.DAY_OF_YEAR, -(n - 2)); 导致计算自然周最后一天周日统计时,搜索的范围是下一周的时间段

2.年月日由于没有转化date型,而是直接写的sql所以“2007-9-7”没有匹配“2007-09-07”

private String andDate(String sql, String limit) {
   if (limit != null && !"".equals(limit)) {
    Calendar cal = Calendar.getInstance();
    String month_str = "";
    String day_str = "";

    int y = cal.get(Calendar.YEAR);
    int m = cal.get(Calendar.MONTH) + 1;
    int d = cal.get(Calendar.DAY_OF_MONTH);
    int max_day = cal.getMaximum(Calendar.DAY_OF_MONTH);
    int n = cal.get(Calendar.DAY_OF_WEEK);

    if (limit.equals("currentmonth")) {
     if (m < 10) {
      month_str = "0" + m;
     } else {
      month_str = "" + m;
     }
     if (max_day < 10) {
      day_str = "0" + max_day;
     } else {
      day_str = "" + max_day;
     }
     String date1 = y + "-" + month_str + "-01";
     String date2 = y + "-" + month_str + "-" + day_str;

     sql = sql + " AND OD.ORDERDATE BETWEEN '" + date1 + "' AND '"
       + date2 + "'";
    } else if (limit.equals("currentweek")) {
     cal.roll(Calendar.DAY_OF_YEAR, -(n - 1)); //注意一定要减1

     y = cal.get(Calendar.YEAR);
     m = cal.get(Calendar.MONTH) + 1;
     d = cal.get(Calendar.DAY_OF_MONTH);

     if (d < 10) {
      day_str = "0" + d;
     } else {
      day_str = "" + d;
     }
     if (m < 10) {
      month_str = "0" + m;
     } else {
      month_str = "" + m;
     }
     String date1 = y + "-" + month_str + "-" + day_str;

     cal.roll(Calendar.DAY_OF_YEAR, 6);

     y = cal.get(Calendar.YEAR);
     m = cal.get(Calendar.MONTH) + 1;
     d = cal.get(Calendar.DAY_OF_MONTH);
     if (m < 10) {
      month_str = "0" + m;
     } else {
      month_str = "" + m;
     }
     if (d < 10) {
      day_str = "0" + d;
     } else {
      day_str = "" + d;
     }
     String date2 = y + "-" + month_str + "-" + day_str;

     sql = sql + " AND OD.ORDERDATE BETWEEN '" + date1 + "' AND '"
       + date2 + "'";
    }
   }
   return sql;
}


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: Calendar 日期
相关日志:
评论: 0 | 引用: 0 | 查看次数: 725
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭