Java: How do I get the date of x day in a month ( e.g. Third Monday in February 2012)

前端 未结 8 1649
执笔经年
执笔经年 2020-12-09 10:44

I am somewhat struggling with this.

I want to setup my Calendar to let\'s say: Third Monday in February 2012. And I didn\'t find any way of doing th

8条回答
  •  我在风中等你
    2020-12-09 11:45

    public String nDow(int year, int month, int nweek, int nday)
    {
        Calendar cdt = Calendar.getInstance();
        cdt.set(year, month -1, 1);
        return year + "-" + month + "-" + (getPosOfWeekday(cdt.get(Calendar.DAY_OF_WEEK), nday) + ((nweek - 1) *7));
    }
    
    private int getPosOfWeekday(int startday, int nday)
    {
        nday = weekDayValue(nday);
        return constructCircularArray(startday).indexOf(nday) + 1;
    }
    
    private ArrayList constructCircularArray(int weekday)
    {
        ArrayList circularArray = new ArrayList();
        for(int i = 0; i < 7; i++)
        {
            circularArray.add(i, weekDayValue(weekday++));
        }
        return circularArray;
    }
    
    private int weekDayValue(int x)
    {
        return ((x-1) % 7) + 1;
    }
    

提交回复
热议问题