我有几个要求,我需要验证今天的日期是否与条件匹配,如果是,则在数据库中创建记录。
但条件是这样的 - 每两周一次,每两周一次。 任何人都可以提供如何实现这一目标吗?
答案 0 :(得分:0)
private static Calendar cacheCalendar;
public static LocalDate getNDayOfMonth(int dayweek, int nthweek, int month, int year) {
LocalDate d = new LocalDate(year, month, 1).withDayOfWeek(dayweek);
if (d.getMonthOfYear() != month)
d = d.plusWeeks(1);
return d.plusWeeks(nthweek - 1);
}
public static LocalDate getLastWeekdayOfMonth(int dayweek, int month, int year) {
LocalDate d = new LocalDate(year, month, 1).plusMonths(1).withDayOfWeek(dayweek);
if (d.getMonthOfYear() != month)
d = d.minusWeeks(1);
return d;
}
public static void main(String[] args) {
cacheCalendar = Calendar.getInstance();
for (int i = 1; i < 13; i++) {
// second wednesday of oct-2011
LocalDate secondMonday = getNDayOfMonth(DateTimeConstants.MONDAY, 2, i, cacheCalendar.get(Calendar.YEAR));
System.out.println("secondMonday= " + secondMonday);
LocalDate secondTuesday = getNDayOfMonth(DateTimeConstants.TUESDAY, 2, i,
cacheCalendar.get(Calendar.YEAR));
System.out.println("secondTuesday= " + secondTuesday);
LocalDate monthlyTuesday = getNDayOfMonth(DateTimeConstants.TUESDAY, 1, i,
cacheCalendar.get(Calendar.YEAR));
System.out.println("monthlyTuesday= " + monthlyTuesday);
LocalDate weeklyFirstFriday = getNDayOfMonth(DateTimeConstants.FRIDAY, 1, i,
cacheCalendar.get(Calendar.YEAR));
System.out.println("weeklyFirstFriday= " + weeklyFirstFriday);
}
}