计算周: getDateInWeek()和DateAdd()两个方法
//返回一个日期字符串在星期中的顺序
public static int getDateInWeek(String strDate) {
DateFormat df = DateFormat.getDateInstance();
try {
df.parse(strDate);
java.util.Calendar c = df.getCalendar();
int day = c.get(c.DAY_OF_WEEK) - c.SUNDAY;
return day;
}
catch (ParseException e) {
return -1;
}
}
//一个日期上加天数
public static String DateAdd(String startDate, int dd) {
java.text.DateFormat df = java.text.DateFormat.getDateInstance();
java.util.Date date = new java.util.Date();
try {
date = df.parse(startDate);
}
catch (Exception ex) {
System.out.print(ex);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, dd);
String mm = String.valueOf(calendar.get(Calendar.MONTH) + 1);
if (mm.length() == 1) {
mm = "0" + mm;
}
String day = String.valueOf(calendar.get(Calendar.DATE));
if (day.length() == 1) {
day = "0" + day;
}
String returnDate = String.valueOf(calendar.get(Calendar.YEAR)) + "-" + mm
+ "-" + day;
return returnDate;
}
计算月:getCurrentTime("yyyy-MM");返回年-月格式的时间.再将数据库中时间substring(),然后比较.
public static String getCurrentTime(String style) {
SimpleDateFormat format = new SimpleDateFormat(style);
return format.format(new Timestamp(System.currentTimeMillis()));
}