Get starting date of week(configurable starting day of week)

前端 未结 4 366
暖寄归人
暖寄归人 2021-01-20 04:57

I have current date, and a constant which tells from which day the week starts. I want to get the start date of the week based on that constant. If I hardcode the first day

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-20 05:19

    I used the following method:

    /** 1 = Sunday, 2 = Monday, 3 = Tuesday, 4 = Wednesday, 5 = Thursday,
    * 6 = Friday, 7 = Saturday
    */
    public static Date getFirstDayOfWeekDate(int firstDay)
    {
        // Calculate the date of the first day of the week
    
        // First get the today's date
        Calendar c = new GregorianCalendar();
    
        // Now set the day of week to the first day of week
        while (c.get(Calendar.DAY_OF_WEEK) != firstDay)
        {
            c.add(Calendar.DAY_OF_MONTH, -1);
        }
    
        return c.getTime();
    }
    

提交回复
热议问题