Get Last Friday of Month in Java

后端 未结 16 1941
一生所求
一生所求 2020-11-28 11:28

I am working on a project where the requirement is to have a date calculated as being the last Friday of a given month. I think I have a solution that only uses standard Ja

16条回答
  •  独厮守ぢ
    2020-11-28 12:01

    public static int lastSundayDate()
    {
        Calendar cal = getCalendarInstance();
        cal.setTime(new Date(getUTCTimeMillis()));
        cal.set( Calendar.DAY_OF_MONTH , 25 );
        return (25 + 8 - (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY ? cal.get(Calendar.DAY_OF_WEEK) : 8));
    }
    

提交回复
热议问题