Java根据年月日判断周几
Java TimeUtil 时间工具类整理,便于下次的敏捷、快速开发。
public class TimeUtil {
/**
* 根据日期获得星期
*
* @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 weekDaysName[intWeek];
}
/**
* 基姆拉尔森计算公式根据日期判断星期几
*
* @param y
* @param m
* @param d
* @return
*/
public static String getWeekOfDate(int y, int m, int d) {
String[] weekDaysName = {"日", "一", "二", "三", "四", "五", "六"};
if (m == 1 || m == 2) {
m += 12;
y--;
}
int iWeek = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
switch (iWeek) {
case 0:
return weekDaysName[1];
case 1:
return weekDaysName[2];
case 2:
return weekDaysName[3];
case 3:
return weekDaysName[4];
case 4:
return weekDaysName[5];
case 5:
return weekDaysName[6];
case 6:
return weekDaysName[0];
default:
return "";
}
}
}