1

嗨,我想在java中制作一个程序,其中days,weekNo是参数..就像每月的第一个星期五或每月的第二个星期一..它返回日期

4

4 回答 4

5

这是一个使用DateUtilsApache Commons / Lang的实用方法:

/**
 * Get the n-th x-day of the month in which the specified date lies.  
 * @param input the specified date
 * @param weeks 1-based offset (e.g. 1 means 1st week)
 * @param targetWeekDay (the weekday we're looking for, e.g. Calendar.MONDAY
 * @return the target date
 */
public static Date getNthXdayInMonth(final Date input,
    final int weeks,
    final int targetWeekDay){

    // strip all date fields below month
    final Date startOfMonth = DateUtils.truncate(input, Calendar.MONTH);
    final Calendar cal = Calendar.getInstance();
    cal.setTime(startOfMonth);
    final int weekDay = cal.get(Calendar.DAY_OF_WEEK);
    final int modifier = (weeks - 1) * 7 + (targetWeekDay - weekDay);
    return modifier > 0
        ? DateUtils.addDays(startOfMonth, modifier)
        : startOfMonth;
}

测试代码:

// Get this month's third thursday
System.out.println(getNthXdayInMonth(new Date(), 3, Calendar.THURSDAY));

// Get next month's second wednesday:
System.out.println(getNthXdayInMonth(DateUtils.addMonths(new Date(), 1),
    2,
    Calendar.WEDNESDAY)
);

输出:

2010 年 11 月 18 日星期四 00:00:00 CET 2010 年
12 月 8 日星期三 00:00:00 CET


这是相同代码的JodaTime版本(我以前从未使用过 JodaTime,所以可能有更简单的方法):

/**
 * Get the n-th x-day of the month in which the specified date lies.
 * 
 * @param input
 *            the specified date
 * @param weeks
 *            1-based offset (e.g. 1 means 1st week)
 * @param targetWeekDay
 *            (the weekday we're looking for, e.g. DateTimeConstants.MONDAY
 * @return the target date
 */
public static DateTime getNthXdayInMonthUsingJodaTime(final DateTime input,
    final int weeks,
    final int targetWeekDay){

    final DateTime startOfMonth =
        input.withDayOfMonth(1).withMillisOfDay(0);
    final int weekDay = startOfMonth.getDayOfWeek();
    final int modifier = (weeks - 1) * 7 + (targetWeekDay - weekDay);
    return modifier > 0 ? startOfMonth.plusDays(modifier) : startOfMonth;
}

测试代码:

// Get this month's third thursday
System.out.println(getNthXdayInMonthUsingJodaTime(new DateTime(),
    3,
    DateTimeConstants.THURSDAY));

// Get next month's second wednesday:
System.out.println(getNthXdayInMonthUsingJodaTime(new DateTime().plusMonths(1),
    2,
    DateTimeConstants.WEDNESDAY));

输出:

2010-11-18T00:00:00.000+01:00
2010-12-08T00:00:00.000+01:00

于 2010-11-09T09:21:40.793 回答
2
  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;
  }

调用代码

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

输出

Mon Dec 06 15:09:00 IST 2010
于 2010-11-09T08:13:39.700 回答
1
于 2017-01-31T21:33:44.630 回答
-1
Calendar cal = Calendar.getInstance();
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);

这应该做你想做的。
编辑:通过更多的计算步骤,您可能会得到结果:)(抱歉混淆了您的标题)

于 2010-11-09T08:11:14.370 回答