java获取星期
Java获取星期
--------------------编程问答--------------------
/**
* 获取星期名称
*
* @param date
* 日期
* @return
*/
public static String getDayweek(Date date) {
String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayinweek = cal.get(Calendar.DAY_OF_WEEK) - 1;
return weeks[dayinweek];
}
/**
* 获取当前日期起始周(星期一)
*
* @param date
* 日期
* @return
*/
public static Date getWeekStart(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_WEEK) - 1;
cal.add(Calendar.DATE, -day + 1);
return cal.getTime();
}
/**
* 获取当前日期周末(星期日)
*
* @param date
* 日期
* @return
*/
public static Date getWeekEnd(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_WEEK) - 1;
cal.add(Calendar.DATE, 6 - day + 1);
return cal.getTime();
}
补充:Java , Java EE