public boolean chackyear(int str) {// 判断平年,闰年
boolean q = str % 4 == 0;
boolean w = str % 100 == 0;
boolean e = str % 400 == 0;
boolean r = false;
if (q) {
if (w == false || e) {
r = true;
}
}
return r;
}
public String chackday(int year, int month, int day) {
int c = 0;// 月的代码
int s = 0;// 21世纪年的代码
int b = 20;// 年代
int y3 = 0;// 年的后两位
int i = 0;// 年份数组下标i
String y = String.valueOf(year);
String y1 = y.substring(2, 4);// 提取年后两位
int y4 = Integer.parseInt(y1);// 转换年份格式
String y2 = y.substring(0, 2);// 提取年份前两位
y3 = Integer.parseInt(y2);// 转换年份前两位格式
int[] k = { 5, 3, 1, 0 };// 年份代码数组
int[] l = { 1, 3, 5, 0 };// 年份代码数组
if (y3 > b) {// 判断 :与21世纪比较
i = (y3 - b) % 4 - 1;
s = ((y4 / 4) + y4) % 7 + k[i];
} else if (y3 < b) {
i = (b - y3) % 4 - 1;
s = ((y4 / 4) + y4) % 7 + l[i];
} else {
i = 3;
s = ((y4 / 4) + y4) % 7 + l[i];
}
// r-2||+5 18 1777
//
// r+3 19 1888
// r+1 20 1999
// r 21 2010
if (chackyear(year) == false) {// 判断平年,闰年
if (month == 5) {// 月份代码
c = 0;
} else if (month == 8) {
c = 1;
} else if (month == 2 | month == 3 || month == 11) {
c = 2;
} else if (month == 6) {
c = 3;
} else if (month == 12 || month == 9) {
c = 4;
} else if (month == 4 || month == 7) {
c = 5;
} else if (month == 1 || month == 10) {
c = 6;
}
} else {
if (month == 5) {
c = 0;
} else if (month == 2 || month == 8) {
c = 1;
} else if (month == 3 || month == 11) {
c = 2;
} else if (month == 6) {
c = 3;
} else if (month == 12 || month == 9) {
c = 4;
} else if (month == 1 || month == 4 || month == 7) {
c = 5;
} else if (month == 1 || month == 10) {
c = 6;
}
}
int r = (s + c + day) % 7;// 星期计算
String result = "";// 初始化变量
switch (r) {// 判断星期几
case 0:
result = "星期日";
break;
case 1:
result = "星期一";
break;
case 2:
result = "星期二";
break;
case 3:
result = "星期三";
break;
case 4:
result = "星期四";
break;
case 5:
result = "星期五";
break;
case 6:
result = "星期六";
break;
default:
break;
}
return result;
}
原文:http://www.cnblogs.com/jimoqiongyin/p/4793320.html