如何获取一个月的第一个星期三的日历

时间:2012-03-24 18:34:39

标签: java calendar

如何使用java日历类获取下个月的第一个星期三的日期。例如:

Today(24/03/2012) the next first Wednesday will be 04/04/2012
On(05/04/2012) the next first Wednesday will be 02/05/2012

感谢。

6 个答案:

答案 0 :(得分:3)

这有效

  • 设定开始日期
  • 移至下个月
  • 移至月初的第一天
  • 在我们到达周三之前添加天数

代码

import static java.util.Calendar.*;

public static void main(String[] args) {
    System.out.println(getNextMonthFirstWed(new Date(112, 3 - 1, 24)));
    System.out.println(getNextMonthFirstWed(new Date(112, 4 - 1, 05)));
}

private static Date getNextMonthFirstWed(Date date) {
    Calendar c = getInstance();
    c.setTime(date);
    c.add(MONTH, 1);
    c.set(DAY_OF_MONTH, 1);

    // search until wednesday
    while (c.get(DAY_OF_WEEK) != WEDNESDAY) {
        c.add(DAY_OF_MONTH, 1);
    }
    return c.getTime();
}

答案 1 :(得分:3)

在Java 8及更高版本中,我们可以使用java.time类,包括LocalDateTemporalAdjusters以及DayOfWeek枚举。请参阅Tutorial

LocalDate firstSundayOfNextMonth = LocalDate
              .now()
              .with( TemporalAdjusters.firstDayOfNextMonth() )
              .with( TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY) );

答案 2 :(得分:2)

也许这段代码是您所需要的:

    Calendar calendar = Calendar.getInstance();
    while (calendar.get(Calendar.DAY_OF_MONTH) > 7 ||
            calendar.get(Calendar.DAY_OF_WEEK) != Calendar.WEDNESDAY) {
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }
    SimpleDateFormat gm = new SimpleDateFormat("MMM yyyy dd");
    System.out.println(gm.format(new Date(calendar.getTimeInMillis())));

今天的输出是:Apr 2012 04。 对于Apr 2012 04,输出为Apr 2012 04。 对于Apr 2012 05,输出为May 2012 02

答案 3 :(得分:2)

此代码将在提供更多功能的同时回答您的问题。使用此功能,您可以指定特定的工作日,并在下个月返回指定工作日的第一次出现。这也避免了每天循环和检查,直到找到正确的一个。我将其转换为我目前正在处理的一些常规代码。希望这有帮助!

private static Date getNextMonthFirstWeekDayOccurence(Date date, int weekDay) {
            //Get Calendar and move it to the first day of the next month
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);

            //Calculate the difference between the days of the week
            int dayDifference = calendar.get(Calendar.DAY_OF_WEEK) - weekDay;

            if (dayDifference < 0) { //Implies (calendar.get(Calendar.DAY_OF_WEEK) < weekDay)
                calendar.add(Calendar.DAY_OF_MONTH, Math.abs(dayDifference));
            } else if (dayDifference > 0) { //Implies (calendar.get(Calendar.DAY_OF_WEEK) > weekDay)
                def daysToAdd = calendar.get(Calendar.DAY_OF_WEEK) - dayDifference;
                calendar.add(Calendar.DAY_OF_MONTH, daysToAdd);
            }
            return calendar.getTime();
    }

答案 4 :(得分:0)

Joda版本:

private static DateTime nextFirstWednesday(DateTime dateTime) {
    while (dateTime.getDayOfMonth() > 7 || dateTime.getDayOfWeek() != DateTimeConstants.WEDNESDAY) {
        dateTime = dateTime.plusDays(1);
    }
    return dateTime;
}

答案 5 :(得分:-1)

public boolean firstWednesdayOfTheMonth() {
        // gets todays date
        Calendar calendar = Calendar.getInstance();
        // get the date
        int date = calendar.get(Calendar.DAY_OF_MONTH);
        // get the day of the week SUNDAY == 1, MONDAY == 2 ....
        int day =   calendar.get(Calendar.DAY_OF_WEEK); 
        // this checks if its a wednesday and the date falls within 8 then it should be the first wednesday
        if (date < 8 &&  day == 4) {
                return true;
        }

            return false;   
    }