java取得指定日期是星期几的方法:(相关视频教程推荐:java视频教程)
1、使用Calendar类
//根据日期取得星期几public static String getWeek(Date date){String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};Calendar cal = Calendar.getInstance();cal.setTime(date);int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;if(week_index<0){week_index = 0;} return weeks[week_index];}
Calendar类星期数成员变量SUNDAY-SATURDAY的值为1-7,一周的开始是周日。
2、使用SimpleDateFormat类
//根据日期取得星期几public static String getWeek(Date date){ SimpleDateFormat sdf = new SimpleDateFormat("EEEE");String week = sdf.format(date);return week;}
注:格式化字符串存在区分大小写
对于创建SimpleDateFormat传入的参数:EEEE代表星期,如“星期四”;MMMM代表中文月份,如“十一月”;MM代表月份,如“11”;
yyyy代表年份,如“2010”;dd代表天,如“25”