How to get first day of a given week number in Java

前端 未结 7 676
独厮守ぢ
独厮守ぢ 2020-12-01 12:38

Let me explain myself. By knowing the week number and the year of a date:

Date curr = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(curr);
i         


        
7条回答
  •  情歌与酒
    2020-12-01 13:09

    Try the Gregorian Calendar algorithm:

    public int getFirstDay(int m, int year)
        {
            int k=1;
            int c, y, w, M=0;
            if(m>2 && m<=12) M=m-2;
            else if(m>0 && M<=2)
            {
                M=m+10;
                year-=1;
            }
            c=year/100;
            y=year%100;
            w=(int)((k+(Math.floor(2.6*M - 0.2))-2*c+y+(Math.floor(y/4))+(Math.floor(c/4)))%7);//a fantastic formula           
            if(w<0) w+=7;
            return w;//thus the day of the week is obtained!
        }
    

提交回复
热议问题