How to get ordinal Weekdays in a Month

前端 未结 4 872
借酒劲吻你
借酒劲吻你 2020-12-22 02:21

hi i want to make a program in java where days,weekNo is parameter ..Like First Friday of the month or second Monday of the month ..and it returns the date

4条回答
  •  孤城傲影
    2020-12-22 02:46

      public static Date getDate(int day, int weekNo, int month, int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DATE,1);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        for (int i = 0; i < 31; i++) {
            if (cal.get(Calendar.WEEK_OF_MONTH) == weekNo
                    && cal.get(Calendar.DAY_OF_WEEK) == day) {
                return cal.getTime();
            }
            cal.add(Calendar.DATE,1);
        }
        return null;
      }
    

    Calling code

    System.out.println(""+getDate(Calendar.MONDAY, 2, Calendar.DECEMBER,2010));
    

    Output

    Mon Dec 06 15:09:00 IST 2010
    
    • Resource
    • Also look at Joda Time it is better

提交回复
热议问题