08-12
09
Calendar获取本月或本周日期
作者:Java伴侣 日期:2008-12-09
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;
}
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;
}
评论: 0 | 引用: 0 | 查看次数: 725
发表评论