Java 计算一周中的哪一天

Java 计算一周中的哪一天,java,Java,我正在尝试编写一个具有世界末日规则的日期类,在该类中,当用户在测试类中输入年、月和日时,它将输出一周中的某一天 通过下面的代码,我可以找出1100年到2900年的末日,但接下来我该怎么办呢 (我是Java新手,所以如果我的代码更干净,请告诉我 private int year; private int month; private int day; public Set <Integer> tue = new HashSet <Integer> (Arrays.asLi

我正在尝试编写一个具有世界末日规则的日期类,在该类中,当用户在测试类中输入年、月和日时,它将输出一周中的某一天

通过下面的代码,我可以找出1100年到2900年的末日,但接下来我该怎么办呢

(我是Java新手,所以如果我的代码更干净,请告诉我

private int year;
private int month;
private int day;

public Set <Integer> tue = new HashSet <Integer> (Arrays.asList(20,16,24,28,12));
public Set <Integer> fri = new HashSet <Integer> (Arrays.asList(18,14,22,26));
public Set <Integer> sun = new HashSet <Integer> (Arrays.asList(21,17,13,25,29));
public Set <Integer> wed = new HashSet <Integer> (Arrays.asList(19,15,23,27,11));

public int getDoomsDay() {
   int y = Integer.parseInt(Integer.toString(this.year).substring(0,2));
   int c;
   if(tue.contains(y)){
       c = 2;
   }
   else if(fri.contains(y)){
       c = 5;
   }
   else if(sun.contains(y)) {
       c = 0;
   }
   else {
       c = 3;
   }
    
   int d = Integer.parseInt(Integer.toString(this.year).substring(2,4));
   int s = d/12;
   int t = d%12;
   int f = t/s;
   int b;
   if((c+s+t+f) > 6) {
       b = (c+s+t+f)%7;
   }
   else {
       b = (c+s+t+f);
   }
   if(this.year % 4 == 0 && this.year % 100 != 0) {
       return b +1;
   }
   else {
       return b;
   }
    
}
private int year;
私人整数月;
私人国际日;
publicsettue=新的HashSet(Arrays.asList(20,16,24,28,12));
publicsetfri=新的HashSet(Arrays.asList(18,14,22,26));
publicsetsun=newhashset(Arrays.asList(21,17,13,25,29));
publicsetwed=新的HashSet(Arrays.asList(19,15,23,27,11));
公共信息获取世界末日(){
int y=Integer.parseInt(Integer.toString(this.year).substring(0,2));
INTC;
如果(星期二包含(y)){
c=2;
}
如果(星期五包含(y)){
c=5;
}
else if(sun.contains(y)){
c=0;
}
否则{
c=3;
}
intd=Integer.parseInt(Integer.toString(this.year).substring(2,4));
int s=d/12;
int t=d%12;
int f=t/s;
int b;
如果((c+s+t+f)>6){
b=(c+s+t+f)%7;
}
否则{
b=(c+s+t+f);
}
如果(this.year%4==0&&this.year%100!=0){
返回b+1;
}
否则{
返回b;
}
}

您可以做很多改进。我将在这里介绍一些:

首先,不要使用字符串获取世纪和世纪中的年份

int century = year / 100;
int yearInCentury = year % 100;
其次,如果我正确理解了算法,你不需要有几个世纪的列表来转换为一周中的某一天。你可以用以下公式计算世界末日:

int anchor = (5 * (century % 4)) % 7 + 2;
第三,

if ((c+s+t+f) > 6) {
    b = (c+s+t+f)%7;
} else {
    b = (c+s+t+f);
}
实际上和

b = (c+s+t+f) % 7;

还有很多其他的改进,但希望这足以回答您的问题。

这里有另一种方法来计算任意日期的一周中的某一天。该公式基于
三月是一年中的第一个月,因此在使用我们的约会系统作为输入时需要进行调整

private static String[] weekdays= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
public static String dayOfWeek(int m, int d, int y) {
    int c = y / 100;
    y = y % 100;
    m = m - 2;
    if (m <= 0) {
        y = y > 0 ? y-1 : 99;
        c--;
        m += 12;
    }
    // basic formula here.
    int wd =  (d + (int) (.2 * (13 * m - 1)) + y + y / 4 + c / 4
            - 2 * c) % 7;
    return weekdays[wd];
}
private static String[]工作日={“Sun”、“Mon”、“Tue”、“Wed”、“Thu”、“Fri”、“Sat”};
公共静态字符串dayOfWeek(int m,int d,int y){
int c=y/100;
y=y%100;
m=m-2;
如果(m0?y-1:99;
c--;
m+=12;
}
//这里的基本公式。
int-wd=(d+(int)(.2*(13*m-1))+y+y/4+c/4
-2*c)%7;
返回工作日[wd];
}

解释和推导并不难理解,但它很长,因此在这里被省略。它来自Oyestein Ore对数论的邀请。幸运的是,这本书的PDF版本在网上。请查看第102-107页。

使用
java.time
并看一看。@andrewjames这个问题可能是一个sc学校分配,因此不允许java.time。