java求13号星期五
作者:互联网
看到外国人怕13号和星期五重叠,于是想看看什么时候会重叠,但没有现成的,于是写了点代码算了一下
public class Firday13 {
public static int dx[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
public static int dy[] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
/**
*闰年判断
*/
public static boolean isYun(Integer year){
if (year%400 == 0 ||(year%4 == 0 && year%100 == 0) ){
return true;
}else {
return false;
}
}
public static void firday(Integer year ,Integer week){
int a[] ;
//循环月份
if (isYun(year)){
a = dy;
}else {
a = dx;
}
for (int i = 1; i < 12; i++) {
for (int j = 1; j <= a[i]; j++,week++) {
if (week>7){
week = week%7;
}
if (week == 5 && j == 13){
System.out.println(year+"年"+i+"月"+j+"日"+"是星期五");
}
}
}
}
/**
* 0是星期天,返回星期几
*/
public static int getWeek(int year){
Calendar calendar = Calendar.getInstance();
calendar.set(year,0,1);
int x = calendar.get(calendar.DAY_OF_WEEK)-1;
return x;
}
public static void main(String[] args) {
for (int i = 1990; i <2100 ; i++) {
firday(i,getWeek(i));
}
}
}
看了两年是准的,一年一两次出现
标签:13,java,int,31,30,星期五,static,year,public 来源: https://blog.csdn.net/w2233997/article/details/122771789