Java判断当前日期是星期几
参考链接地址:http://blog.csdn.net/a9529lty/article/details/3206942
/**
* 判断当前日期是星期几
* @param pTime 修要判断的时间
* @return dayForWeek 判断结果
* @Exception 发生异常
*/
public static int dayForWeek(String pTime) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(format.parse(pTime));
int dayForWeek = 0;
if (c.get(Calendar.DAY_OF_WEEK) == 1) {
dayForWeek = 7;
} else {
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
return dayForWeek;
}
/**
* @param dt
* @return 当前日期是星期几
*/
public static String getWeekOfDate(Date dt) {
String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 根据日期获得星期
*
* @param date
* @return
*/
public static String getWeekOfDate(String date) {
if (StringUtils.isEmpty(date)) {
return "";
}
String[] weekDaysName = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date dt1;
try {
dt1 = df.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dt1);
int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
return weekDaysName[intWeek];
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}