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

前端 未结 7 670
独厮守ぢ
独厮守ぢ 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:04

    I haven't done much Date stuff in java but a solution might be:

    cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_WEEK));
    

    Logic:

    Get the day of the week and substract it from the current date (might need -1, depending on wether you need monday to be first day of the week or sunday)

提交回复
热议问题