0

最近、2012 年 12 月は特別な月であるというメールを受け取りました。今月は 5 つの土曜日、5 つの日曜日、5 つの月曜日があります。また、これは約700年に一度だけ起こると言われています。では、2000 年から 2100 年の間に、土曜、日曜、月曜がそれぞれ 5 回ある月が何月になるかを確認する必要があります。

これは Java を介して (特に Calander API を使用して) 実現できますか?

サンプル コードを使用した任意のアイデアは、かなりの価値があります。

ありがとう。

4

3 に答える 3

6

任意の月に (土曜日、日曜日、月曜日) の 5 つが含まれるには、次の条件が必要です。

  • 31 日間 (丸 4 週間と 3 日間余分に)
  • 土曜日開始(最後の 3 日間は第 5 土曜日、日曜日、月曜日でなければなりません)

これらのチェックは両方とも、根本的に壊れたjava.util.CalendarAPI でもかなり簡単なはずです。(選択肢がある場合はJoda Timeを優先してください。) それらのいずれかを実装するのに問題がある場合は、特定の問題を投稿して、これまでにどれだけ進んだかを示してください。

于 2012-11-12T07:28:25.010 に答える
1

以下のコードを確認してください。

/**
 * Print all the months which has five Mondays, Saturdays and Sundays
 * @param from year from
 * @param to year to
 */
public void printMonths(int from, int to) {
    List<String> monthList = new ArrayList<String>();
    for (int year =from; year<=to;year++) {
        monthList.addAll(getMonth(year));
    }

    for (String s : monthList) {
        System.out.println(s);
    }
}

/**
 * Get month with five sundays, saturdays and mondays
 * @param year
 * @return
 */
private List<String> getMonth(int year) {
    List<String> monthList = new ArrayList<String>();
    for (int month = 0; month < 12; month++) {
        if (check(year, month)) {
            monthList.add("" + year + "-" + (month+1));
        }
    }
    return monthList;       
}

private boolean check(int year, int month) {
    return checkFiveDays(year, month, 1) && checkFiveDays(year, month, 2) 
    && checkFiveDays(year, month, 7);

}

private boolean checkFiveDays(int year, int month, int dayOfWeek) {
    Calendar c = Calendar.getInstance();
    c.set(year, month, 0, 0, 0, 0);
    int times = 0;
    do {
        if (c.get(Calendar.DAY_OF_WEEK) == dayOfWeek) {
            times++;
        }
        c.add(Calendar.DAY_OF_MONTH, 1);
    } while (c.get(Calendar.MONTH)== month);

    return times == 5;
}
于 2012-11-12T07:50:21.760 に答える
-1

実際には、1 か月に 5 日 (土曜日、日曜日、または月曜日) があるのに、31 日である必要はありません。月がその日に始まるわけでもありません。土曜日の場合は 2013 年 3 月を参照してください。
Joda を使用すると、次のように 2000 年から 2100 年までのすべてのケースを簡単にカウントできます。

DateTimeFormatter dateParser = DateTimeFormat.forPattern("yyyy");                                                            
DateTime startDate = dateParser.parseDateTime("2000");                                                                       
DateTime stopDate = dateParser.parseDateTime("2100");                                                                        
DateTime date = startDate;                                                                                                   
DateTime lastInMonth;                                                                                                        
if (date.getDayOfWeek() != DateTimeConstants.SATURDAY)                                                                       
    date = date.plusWeeks(1).withDayOfWeek(DateTimeConstants.SATURDAY);                                                      
int count = 0;                                                                                                               
while (date.isBefore(stopDate)) {                                                                                            
    if (date.getDayOfWeek() != DateTimeConstants.SATURDAY) //if it's not starting on Saturday                                
        date = date.plusWeeks(1).withDayOfWeek(DateTimeConstants.SATURDAY); //jump to the first Saturday                     
    lastInMonth = date.plusWeeks(4); //jump 4 weeks                                                                          
    if (date.getMonthOfYear() == lastInMonth.plusDays(2).getMonthOfYear()) { //check if we are still on the same month       
        count++;                                                                                                             
        // System.out.println("date = " + date); //uncomment to see each such month                                             
    }                                                                                                                        
    date = date.plusMonths(1); //advance a month                                                                             
    date = date.withDayOfMonth(1); //start on the first day                                                                  
}                                                                                                                            
System.out.println("Between " + startDate + " and " + stopDate + " there are " + count + " months that have five Saturdays");

once in some 700 years only12 月だけを数えたとしても、これは正確ではありません。

于 2012-11-12T09:49:05.283 に答える