/**
* 根据日期获得星期
* @param date
* @return
*/
public static String getWeekOfDate(Date date) {
String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
return weekDaysCode[intWeek];
}
/**
* 获得周一的日期
*
* @param date
* @return
*/
public static String getMonday(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return dateFormat.format(calendar.getTime());
}
/**
* 获得周三的日期
*
* @param date
* @return
*/
public static String getWednesday(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
return dateFormat.format(calendar.getTime());
}
/**
* 获得周五的日期
*
* @param date
* @return
*/
public static String getFriday(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
return dateFormat.format(calendar.getTime());
}
/**
* 当前日期前几天或者后几天的日期
* @param n
* @return
*/
public static String afterNDay(int n) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DATE, n);
Date date = calendar.getTime();
String s = dateFormat.format(date);
return s;
}
/**
* 判断两个日期是否在同一周
*
* @param date1
* @param date2
* @return
*/
public static boolean isSameWeekDates(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
if (0 == subYear) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
.get(Calendar.WEEK_OF_YEAR))
return true;
} else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
.get(Calendar.WEEK_OF_YEAR))
return true;
} else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
.get(Calendar.WEEK_OF_YEAR))
return true;
}
return false;
}